Memory App
Programming
 




Downsides

One of the downsides of the way PHP handles variables is that there is no way to ensure that any one of them will exist at any given point in the execution of a script. 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 
Undefined variable

error_reporting(E_ALL);

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

if (!empty($a)) {
    echo 'Yes'; // Output: nothing (better)
}
(1/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
empty() - determine whether a variable is empty

// Determine whether a variable is considered to be 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
It only checks variables as anything else will result in a parse error

$a = 1;
empty(trim($a)); // Fatal error


Questions



Top Reference > Programming
0/0 (80)  
Not Logged