jueves, 10 de noviembre de 2016

empty() / isset() is_null comparison.

Comparisons of $x with PHP functions
Expressiongettype()empty()is_null()isset()boolean : if($x)
$x = "";stringTRUEFALSETRUEFALSE
$x = null;NULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x is undefinedNULLTRUETRUEFALSEFALSE
$x = array();arrayTRUEFALSETRUEFALSE
$x = array('a', 'b');arrayFALSEFALSETRUETRUE
$x = false;booleanTRUEFALSETRUEFALSE
$x = true;booleanFALSEFALSETRUETRUE
$x = 1;integerFALSEFALSETRUETRUE
$x = 42;integerFALSEFALSETRUETRUE
$x = 0;integerTRUEFALSETRUEFALSE
$x = -1;integerFALSEFALSETRUETRUE
$x = "1";stringFALSEFALSETRUETRUE
$x = "0";stringTRUEFALSETRUEFALSE
$x = "-1";stringFALSEFALSETRUETRUE
$x = "php";stringFALSEFALSETRUETRUE
$x = "true";stringFALSEFALSETRUETRUE
$x = "false";stringFALSEFALSETRUETRUE



OTHER TOPIC
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)

A simple empty() / isset() comparison.
<?php
$var 
0;// Evaluates to true because $var is emptyif (empty($var)) {
    echo 
'$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is setif (isset($var)) {
    echo 
'$var is set even though it is empty';
}
?>

No hay comentarios:

Publicar un comentario