PROGRAMMING

 
REMEMBERS




Last update:   29-10-2021

Downsides

There are downsides of the way PHP handles variables. There is no way to ensure that any one of them will exist at any given point. A call to isset() will return TRUE if a variable exists and has a value other than NULL A call to empty() will returns TRUE if var has a empty and zero value ("", 0, NULL)
 
if(isset($_POST['myField']) && $_POST[myField] != "")
    ... code

if(!empty($_POST['myField'])) // a more efficient way
    ... code 
With error reporting, the result is undefined variable
 
error_reporting(E_ALL);

if ($a) {
    echo 'Yes'; // Notice: Undefined variable: a
}

if (!empty($a)) {
    echo 'Yes'; // Output: nothing (better)
}
... 2 lines
 
Check    (2/2)

Check

isset() - determine if a variable is set and is not NULL
 
$a = 1;
$b = 0;
$c = "";
$d = null;

echo isset($a); // TRUE

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

echo isset($a, $b, $c); // TRUE

echo isset($a, $b, $c, $d); // FALSE
... 3 lines
 
empty() - determine whether a variable is empty
 
$a = ""; echo empty($a); // TRUE

$a = 0; echo empty($a); // TRUE

$a = "0"; echo empty($a); // TRUE

$a = NULL; echo empty($a); // TRUE

$a = FALSE; echo empty($a); // TRUE

$a = array(); echo empty($a); // TRUE
... 1 lines
 
Empty() checks only variables as anything else will result in a parse error
 
$a = 1;

echo empty(trim($a)); // Fatal error

Questions    
Type casting

        A B C D E F
🔔
1/2