php - Usage of "isset" function
Get the solution ↓↓↓Although I visit php documentation , I didn't understand usage of "isset" function . 1 - for example in a php tutorial book author wrote a text with this context : when we create a form in a first.html and we want to use from form information in second.php , we must use these 2 pieces of code :
if(!isset($_GET['q']))
die("The Search key word is not set !!!");
$key = $_GET['q'];
if($key == "")
die("The Search key word must be entered !!!");
but I don't understand what is difference between these 2 codes ?
2 - for another example I saw this code for checking that a bottom is clicked or not :
if(isset($_POST['login_btn']))
{
....
}
but I don't understand why does it check that a bottom is clicked or not ?
Answer
Solution:
$key="";
isset($key) //This evaluates to true.
The string is empty, but the variable is defined. The function returns true, even for an empty string.
Perhaps you would like to use instead, which has a broader range of conditions that evaluate to false.
Answer
Solution:
isset
checks to see if a given variable or index exists. If you assume that a variable exists and it doesn't, PHP generates a notice. All you're doing by usingisset
is suppressing possible notices -- which is good practice.
Answer
Solution:
The difference is, in one instance the form was not submitted, in the second instance, the form was submitted, but with a blank value.
You wouldn't want to process a form on your page if the form has not been submitted yet.
There may also be multiple forms on one page, so you might check the button value to find out which form was submitted.
Answer
Solution:
This function is quite simple
As on php.net:
Determine if a variable is set and is not NULL.
In your case, the isset function checks of your post variable is empty or not
Answer
Solution:
$key == ""; // Is $key an empty string
isset($key); // Has the $key variable been defined and not null
And just for reference, here are a few others that may be useful
empty($key); // Is $key and empty value (0, false, null, "", array())
$key == false // Is $key a falsey value (0, false, null, "", "0", array())
$key === false // Is $key a false boolean (false)
Answer
Solution:
I'm just going to dissect your code a little bit..
isset
{-code-1}
Would typically be used to see if a variable in a URL is set so something like this:
http://mysite.com/index.php?q=1
Would have$_GET['q']
set andisset($_GET['q'])
would come back true
$key==""
$key = $_GET['q'];
if($key == "")
Would check to see if$key
is empty. Using my previous example URL,$key
would not be empty or blank, it would be1
.
Why check for a button press
The script that processes your form needs to know that it is being accessed after the form that way it does not error out. This is where you would want to make sure that the form submit button was pressed. As this is confusing, here is an example:
Say you want to insert a tag for your blogging system into a database you might have code that looks like this:
AddTag.php
<form name="addtag" method="process.php" action="post">
<input type="text" name="tagname" />
<input type="submit" name="submittag" />
</form>
process.php
<?php
if ($_POST['submittag']) {
//INSERT query
}
?>
As you can see, if process.php is accessed without the AddTag form being accessed first, the script would not try to insert your tag.
Answer
Solution:
Everyone here has already explained to you what theisset()
function checks for (that being if a variable is set or not), but I think what you're really asking about is how$_GET[]
and$_POST[]
work. You're going to need to look more at the form that feeds this code. Read about what$_GET[]
and$_POST[]
variables are and I think this code will make a lot more sense to you.
For instance, your second example checks to see if the value namedlogin_btn
was sent via post method. If it was, then it runs the ... code block.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.