2016-09-16 2 views
1

Gibt es Unterschiede zwischen leerem() Falsy-Ness-Test und Bool-Typ-Cast?

leer erkennt falsy-ness in den folgenden Fällen:

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

Gibt es andere Werte, die in einer bool Typumwandlung auf False umgewandelt werden?

+1

was ist die Frage? nicht verständlich –

+0

Glaubst du, dass die Liste aus den PHP-Dokumenten falsch ist? –

+0

Bitte beschreiben Sie richtig, was Sie wollen oder was Ihre wahre Frage? –

Antwort

0

Die Antwort liegt in http://php.net/manual/en/types.comparisons.php als @Bert vorgeschlagen

aus der Tabelle auf der Seite ableiten:

+-----------------------+---------+------------------+-------------------+ 
|      |   |     |     | 
+-----------------------+---------+------------------+-------------------+ 
| Expression   | empty() | boolean : if($x) | boolean : if(!$x) | 
| $x = "";    | TRUE | FALSE   | TRUE    | 
| $x = null;   | TRUE | FALSE   | TRUE    | 
| var $x;    | TRUE | FALSE   | TRUE    | 
| $x is undefined  | TRUE | FALSE   | TRUE    | 
| $x = array();   | TRUE | FALSE   | TRUE    | 
| $x = array('a', 'b'); | FALSE | TRUE    | FALSE    | 
| $x = false;   | TRUE | FALSE   | TRUE    | 
| $x = true;   | FALSE | TRUE    | FALSE    | 
| $x = 1;    | FALSE | TRUE    | FALSE    | 
| $x = 42;    | FALSE | TRUE    | FALSE    | 
| $x = 0;    | TRUE | FALSE   | TRUE    | 
| $x = -1;    | FALSE | TRUE    | FALSE    | 
| $x = "1";    | FALSE | TRUE    | FALSE    | 
| $x = "0";    | TRUE | FALSE   | TRUE    | 
| $x = "-1";   | FALSE | TRUE    | FALSE    | 
| $x = "php";   | FALSE | TRUE    | FALSE    | 
| $x = "true";   | FALSE | TRUE    | FALSE    | 
| $x = "false";   | FALSE | TRUE    | FALSE    | 
+-----------------------+---------+------------------+-------------------+ 

was darauf schließen lässt, dass empty() und if(!$x) gleichwertig sind.

Verwandte Themen