What's the difference between 'isset()' and '!empty()' in PHP? - php

I don't understand the difference between isset() and !empty().
Because if a variable has been set, isn't it the same as not being empty?

ISSET checks the variable to see if it has been set. In other words, it checks to see if the variable is any value except NULL or not assigned a value. ISSET returns TRUE if the variable exists and has a value other than NULL. That means variables assigned a "", 0, "0", or FALSE are set, and therefore are TRUE for ISSET.
EMPTY checks to see if a variable is empty. Empty is interpreted as: "" (an empty string), 0 (integer), 0.0 (float)`, "0" (string), NULL, FALSE, array() (an empty array), and "$var;" (a variable declared, but without a value in a class.

Source :http://php.net/manual/en/types.comparisons.phpThis page shows the comparison of the empty(),is_null(),isset().

The type comparison tables gives answer of all question about these operators
http://php.net/manual/en/types.comparisons.php

And one more remark. empty() checks if the variable exists as well. I.e. if we perform empty() to the variable that wasn't declared, we don't receive an error, empty() returns 'true'. Therefore we may avoid isset() if next we need to check if the variable empty.
So
isset($var) && !empty($var)
will be equals to
!empty($var)

isset — Determine if a variable is set and is not NULL.
!empty — Determine whether a variable is NOT empty.

Isset return false if variable has not been set or it is null and return true if variable has been set and not null.
!empty return true if variable has been set and not empty. Empty string, empty array, "0",0 and false are defined as empty.

Use !empty when there is a condition already present which is checking for true or false.
isset is more basic. empty incorporates more checks, hence needs to be used with care.

Related

what is the difference between empty() and $_POST["name"]==""; in php?

i couldn't figured it out what is the real difference functionality between empty() and $_POST["xxx"]==""?
empty() is a statement (unlike any function you could define) which will not trigger an E_NOTICE if called on a variable that is actually undefined. So it also include an isset check.
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
Note that "equals FALSE" means an == comparison, so e.g. empty strings, a string containing a single zero, NULL, empty arrays are all considered empty.
The following things are considered to be empty (return true):
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
BUT
$_POST["xxx"]==""
Return true when $_POST["xxx"] is an empty string

Please can anyone help explain to me the meaning of the following codes?

Please help me understand the following codes:
<?php
session_start();
if(!isset($_SESSION['StaffId'])){
$_SESSION['StaffId']="";
}
?>
does it mean if there is not a set of StaffId, set it to null?
It's checking to see if the key 'StaffId' exists in the $_SESSION array.
For example, if you have an array:
$person = array( 'name' => 'george', 'age' => 22);
Then isset($person['name']) will return true but isset($person['height']) will return false.
So your code sample sets $_SESSION['StaffId'] to '' (an empty string), but only if that key has not been set yet for $_SESSION.
Edit: it's worth noting if you didn't know already that $_SESSION is a variable with special meaning in PHP, see the docs.
It simply checks to see if $_SESSION['StaffId'] is set.
If it is not then it sets it to "" which is an empty string not null
isset - Determine if a variable is set and is not NULL.
If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.
If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

unable to understand the behaviour of the isset and empty in the following form

I have a form with username and password and the value gets posted to check.php
when I have no input in username and password,(left blank), isset($_POST["username"]) and isset($_POST["password"]) returns true, which it should not be as I did not set anything.
But empty($_POST["username"] returns true as is expected.
can someone explain this behaviour of isset.
Thanks
If you submit form elements...
<input name='username'...
...but they're empty, it will fill the $_POST array with empty values by default. So, the variables are set, but they're empty ("").
What I usually do in this case is something like
if (isset($_POST['username'] && $_POST['username']) { ... }
In that case, it will check for if it's set (that is, is the form submitted) and if the values aren't empty. If they're empty ("") PHP will interpret as false and the condition will not be met.
That may be because your $_POST['username'] is set and may be empty, so to overcome check like this
if ((isset($_POST["username"]) && ($_POST["username"]!=""))
isset — Determine if a variable is set and is not NULL.
If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.
If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.
refer :- http://php.net/manual/en/function.isset.php

PHP if (variable)

In various PHP tutorials I see this syntax -
if ($_POST) {
do something
}
I want to know whether this is equivalent to either isset or !(empty) (either one) or has different properties.
It attempts to evaluate the expression and cast it to boolean.
See 'Converting to boolean' at http://php.net/manual/en/language.types.boolean.php to see which values will be equivalent to true and which to false.
It does NOT however check for array key existence (i.e. isset), so if you try if ($_GET['somekey']) but somekey does not exist, you will see PHP Notice: Undefined index: somekey and then false will be assumed.
The best practice would be to perform empty() or isset() checks manually first as fits.
Good question. You are adressing one of PHPs dark sides if you ask me.
The if statement
Like in any other language I can imagine if evaluates the parameter to either true or false.
Since PHP doesn't really know types you could put any expression as parameter which will then be casted to bool as a whole
Following values are considered to be "FALSE"
boolean FALSE
integer 0
float 0.0
empty string
string "0"
any array with zero elements
NULL e.g. unset variables or $var = null
SimpleXML objects when created from empty tags
EVERY other value or expression result is casted to bool TRUE
Now, knowing this, all we need to find out is, what an expression or function returns when executed
If no POST data is set, the following expression would be TRUE
$_POST == FALSE
The isset function
isset returns bool TRUE when the given variable is set and not null.
parameters can be variables, array elements, string offsets and data members of objects.
In PHP 5.4 they fixed the behaviour with string offsets
$var = FALSE;
isset( $var ) === TRUE;
$var === FALSE;
More here
http://de1.php.net/manual/en/function.isset.php
The empty function
Returns false when a variable is considered to be empty or does not exist.
Those values are considered empty:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following values are considered to be empty:
"" (empty string)
0 (integer)
0.0 (float)
"0" (string)
NULL
FALSE
array() (empty array)
Also declared variables without value are empty
compare table
$var = FALSE;
isset($var) === TRUE;
empty($var) === TRUE;
$var === FALSE;
if ($_POST)
This will evaluate to true if there are any elements in the POST array.
if(isset($_POST))
This will always evaluate to true because the POST array is always set, but may or may not contain elements, therefore it is not equivalent to the first example.
if(!empty($_POST))
This however, is equivalent to the first example because empty() checks for contents in the array.
A good generic way of testing if the page was posted to is:
if($_SERVER['REQUEST_METHOD'] == 'POST')
$_POST:
this is used to find whether data is passed on using HTTP POST method and also extracting the variables sent through the same which are collected in an associative array
isset:
checks whether a variable is set(defined) or is NULL(undefined)
PHP.net is an invaluable source of information for figuring out the intricacies and quirks of the language.
With that said, those are not equivalent. The if statement converts the expression to a boolean value (see here for information on what is considered false).
isset is used to "determine if a variable is set and is not NULL."
empty determines whether a variable is empty, i.e., "it does not exist or if its value equals FALSE."
Best practice if you want to check the value of a variable but you aren't sure whether it is set is to do the the following:
if(isset($var) && $var) { ... }
ie check isset() AND then check the variable value itself.
The reason for this is that if you just check the variable itself, as per the example in your question, PHP will throw a warning if the variable is not set. A warning message is not a fatal error, and the message text can be suppressed, but it's generally best practice to write code in such a way that it doesn't throw any warnings.
Calling isset() will only tell you whether a variable is set; it won't tell you anything about the value of the variable, so you can't rely on it alone.
Hope that helps.

In PHP, if a variable does not have a value, why does isset() still return TRUE?

Here is my code:
<?php
$ja = '';
if(isset($ja))
echo "cool!";
?>
I get a "cool!" when running this simple piece of code in my browser. I learned from php.net that
isset — Determine if a variable is set
and is not NULL
Well, in my code, I did declare the variable $ja, but I didn't add any value to it, so shouldn't it be "NULL"?
Even though '' seems like nothing, it still has a value (a NULL character at the end of the string).
isset() checks if the variable is set or not, which in the case (to ''), it is. You may want to set $ja to NULL first beforehand, instead of setting it to an empty string... or use empty() ;)
The empty string is still a value. so you did give it a value which is not null - '' is a perfectly normal string value. perhaps you want ! empty($ja)
Isset is used to tell whether a variable is set or not:
isset($notDefined) //false
$notDefined = 0;
isset($notDefined) //true
(Assuming that $notDefined hasn't been defined before)
To check whether the variable is empty you can use if(empty($var)) or if($var==0)
You did add value to $ja - you set it to an empty string. An empty string is not null.
What you may be confused with is that an empty string and null both evaluate to "false" in PHP when you cast it to Boolean.
PHP's documentation is fairly clear on usage of isset.
The isset function does determine whether or not an object has a value. "NULL" is truly the only way to give an object a value of nothing. $s = '' simply gives an output of nothing. BOOL values(true/false) says that it's yes or no... 0 simply gives the object a int value of 0.
As the name implies of the function, it checks if some variable has been set, in a sense not that it has some value, but in a sense that it has been created. I think the name could be a bit confusing so I will bring a javascript analogy. In javascript to check if the variable exists you do the following:
if (typeof(somevar) == "undefined")
alert("Sorry, the variable has not been set already")
else
alert("Congratulations, the variable has not been set")
So, what you are doing is that you are making a variable $ja, and since by doing so, the variable already exists and therefore has been set.
Hope this helps

Categories