Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will isset() return false if I assign NULL to a variable?

Tags:

php

null

isset

I mean... I "set" it to NULL. So isset($somethingNULL) == true?

like image 536
openfrog Avatar asked Dec 31 '09 15:12

openfrog


People also ask

Does Isset check for NULL?

Definition and Usage The isset() function determines whether a variable is set. To be considered a set, it should not be NULL. Thus, the isset() function also checks whether a declared variable, array or array key has a null value. It returns TRUE when the variable exists and is not NULL; else, it returns FALSE.

Does Isset check for NULL PHP?

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

How do you check if a variable is false in PHP?

is_bool($var): The function is_bool() checks whether a variable's type is Boolean and returns true or false. Comparison using “===” or ($var===true || $var===false): If you compare the variable with the operator “===” with true and false and if it is identical to one of the values, then it is a boolean value.

Should I use isset or empty?

isset() : You can use isset() to determine if a variable is declared and is different than null . empty() : It is used to determine if the variable exists and the variable's value does not evaluate to false . is_null() : This function is used to check if a variable is null .


2 Answers

bool isset ( mixed $var [, mixed $var [, $... ]] )

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.

Return values

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

From the manual. Examples on the same page.

like image 185
Gregory Pakosz Avatar answered Oct 16 '22 09:10

Gregory Pakosz


Yes - from the ISSET() documentation:

$foo = NULL;
var_dump(isset($foo));   // FALSE

/* Array example */
$a = array ('test' => 1, 'hello' => NULL);

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE
like image 30
OMG Ponies Avatar answered Oct 16 '22 09:10

OMG Ponies