Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

1. Null

A variable is considered to be null if:


  • it has been assigned the constant NULL.

  • it has been unset().

  • it has not been set to any value yet.

Note: empty array is converted to null by non-strict equal '==' comparison. Use is_null() or '===' if there is possible of getting empty array.

eg: 1

$a = array();

$a == null  <== return true
$a === null < == return false
is_null($a) <== return false

eg: 2

Note the following:
$test ='';

if (isset($test)){
    echo 'Variable test exists';
}
if (empty($test)){ // same result as $test = ''
    echo ' and is_empty';
}
if ($test == null){ // not the same result as is_null($test)
    echo 'and is_null';
}
The result would be:
Variable test exists and is_empty and is_null

eg : 3
But for the following code...:
$test ='';

if (isset($test)){
    echo 'Variable test exists';
}
if (empty($test)){ // same result as $test = ''
    echo ' and is_empty';
}
if ($test === null){  // same result as is_null($test)
    echo 'and is_null';
}
The result would be:
Variable test exists and is_empty


Therefore, for empty string variables, seems that 'empty' and 'null' has the same value but different type.

2. 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.

empty() is essentially the concise equivalent to !isset($var) || $var == false.

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

"" (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)

eg:
<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}

?>

3. isset()


isset — Determine if a variable is set and is not NULL

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

$a = "test";
$b = "anothertest";

var_dump(isset($a));      // TRUE
var_dump(isset($a, $b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE
var_dump(isset($a, $b)); // FALSE

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

eg:
<?php

$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));

var_dump(isset($a['test']));            // TRUE
var_dump(isset($a['foo']));             // FALSE
var_dump(isset($a['hello']));           // FALSE

// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try:
var_dump(array_key_exists('hello', $a)); // TRUE

// Checking deeper array values
var_dump(isset($a['pie']['a']));        // TRUE
var_dump(isset($a['pie']['b']));        // FALSE
var_dump(isset($a['cake']['a']['b']));  // FALSE


?>

No comments:

Post a Comment

Bottom Ad [Post Page]