Reputation:
What does mean term that $var is set or not set (in PHP or general in programming)?
I found out here http://www.php.net/manual/en/types.comparisons.php some comparison table, but sometime it confuses me.
Does that mean that variable is set when its declared and have its value assigned or not set when it isn't declared or declared but not assigned?
Upvotes: 1
Views: 282
Reputation: 41810
It depends on what you mean by "set".
If you mean "I can use the variable in my code without generating undefined variable notices", then a variable is set when it has any value. You can't declare a variable in PHP without assigning it a value. For example:
<?php
$var;
Does not create the $var
variable. Trying to use that $var
for anything after that line of code will give you an undefined variable notice, and if you print_r(get_defined_vars());
you'll get an empty array.
If you mean "isset($var)
will return true
", then a variable is set when it has any value other than null
. For example:
<?php
$varX = null;
$varY = 0;
With these variables, both are defined. You can use them in your code without getting undefined variable notices. print_r(get_defined_vars());
will show you Array ( [varX] => [varY] => 0 )
. But isset($varX)
returns false
even though $varX
is defined, because it has a null
value.
Once you have assigned a value to a variable, it will remain defined within its scope unless you explicitly unset it with unset($var)
.
The only case where you can declare a variable without explicitly assigning a value is when it is a class property. For example:
class Example
{
public $var;
}
$ex = new Example;
var_dump($ex->var);
Here $var
is implicitly assigned a value of null when it is declared, and referring to it in your code will not cause an undefined variable notice.
Upvotes: 1
Reputation: 1279
isset()
is a function in PHP that will return true if the variable, in this case, $var has been assigned a value. If the variable has been created but nothing assigned, has the value of null or undefined, it will return false. basically, isset($var)
says is this variable safe to use or not.
To explain the differences between a NULL
value and an undefined
the following code was provided by Jonathan Kuhn in the commits above.
<?php
//test 1 is defined, but has a value of null. isset will return false, use causes no error.
$test1 = null;
var_dump($test1);
var_dump(isset($test1));
echo "\n----------\n\n";
//test2 is defined with a string value. isset will return true
$test2 = "test";
var_dump($test2);
var_dump(isset($test2));
echo "\n----------\n\n";
//test3 is not defined, isset returns false and use causes error.
var_dump($test3);
var_dump(isset($test3));
Which will output:
NULL
bool(false)
----------
string(4) "test"
bool(true)
----------
Notice: Undefined variable: test3 in /in/Nmk0t on line 17
NULL
bool(false)
Upvotes: 3
Reputation: 435
This answer is only to clarify a bit more about truty/false
and isset()
function of php, @thatguy posted a great answer, this only provides examples.
These are a few examples about how php works with true/false scenarios
Check the that link for the code to get this output.
You get this output from a loop with this variables:
$var1 = 0;
$var2 = 1;
$var3 = null;
$var4;
$var5 = false;
$var6 = true;
Examples about isset(), and truthy/falsy comparitions
-----------------------------------------------------
About isset() function
----------------------
var1 is declared and has a value, value = 0
var2 is declared and has a value, value = 1
var3 is declared with no value or is null
var4 is declared with no value or is null
var5 is declared and has a value, value =
var6 is declared and has a value, value = 1
----------------------
About true/false cases
----------------------
var1 is declared with no value, is null or equal to zero or false
var2 is declared and has a value, value = 1
var3 is declared with no value, is null or equal to zero or false
var4 is declared with no value, is null or equal to zero or false
var5 is declared with no value, is null or equal to zero or false
var6 is declared and has a value, value = 1
About asking to a never ever writen variable
--------------------------------------------
no idea where this variable is
no idea where this variable is
Upvotes: 1
Reputation: 49
Basically a variable that is not declared, not assigned, or NULL is not set.
To prove the comparison table you can test it with isset()
if (isset($var)) {
echo "it is set.";
}
Upvotes: 1