1

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?

  • 1
    PHP does not declare variables. – SLaks Jun 14 '18 at 20:46
  • `isset()` will return true for variables with a value that is not `NULL` – HTMHell Jun 14 '18 at 20:47
  • @HtmHell It will also be false if the variable is unassigned or undefined. – thatguy Jun 14 '18 at 20:49
  • 1
    @thatguy sure, look again at my comment. I mentioned when it will be TRUE, not false. – HTMHell Jun 14 '18 at 20:50
  • 3
    @thatguy at which point it will have an implicit `NULL` value which still makes his statement true. – Jonathan Kuhn Jun 14 '18 at 20:51
  • @HtmHell yes, but you said if the value if not `NULL`. `Undefined` is not the same as null and will also return a false. – thatguy Jun 14 '18 at 20:53
  • 2
    @thatguy if you try to use a variable that hasn't been defined, it will have a value of `NULL` and emit a notice error. Also, `Undefined` is a javascript thing, not php technically. There really is no `Undefined` state in php. It's either not defined with an implicit `NULL` value (and notice error if you use it before defining) or defined with some value (which could include `NULL` even though `isset` returns false for that). – Jonathan Kuhn Jun 14 '18 at 20:54
  • @thatguy See it for yourself, run the code `var_dump($a);` without assinging it – HTMHell Jun 14 '18 at 21:03
  • [PHP.net manual - $x is undefined](http://www.php.net/manual/en/types.comparisons.php) 4th expression down.If undefined does not exist in PHP then why make references to it all over the place and have an expression for both `undefined` and `NULL` in the examples. – thatguy Jun 14 '18 at 22:03
  • 2
    @thatguy Yes, technically "undefined" does exist in that the variable hasn't been defined yet, but "undefined" isn't actually a real type in php like it is in JavaScript (I assumed you were referring to a specific type, if not then ignore the JS part). It just means that the variable hasn't been declared yet or was unset at some point. Here are the differences between null and undefined: https://3v4l.org/Nmk0t – Jonathan Kuhn Jun 14 '18 at 22:16
  • @JonathanKuhn I was not referring to a type or value of undefined. And yes I am aware that `undefined`is not a value and that everything defaults to a `NULL` value in PHP. My point was that a variable can be `undefined` in PHP and that it is technically different from a variable assigned `NULL`. – thatguy Jun 14 '18 at 22:24

4 Answers4

3

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.

Update

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)
Community
  • 1
  • 1
thatguy
  • 1,249
  • 9
  • 26
1

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.";
}
1

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
Francisco Hahn
  • 435
  • 5
  • 10
1

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80