Re: omit's comment
The note omit quotes is referring to the VALUE returned, not its name. If you put 42 into a text field, the corresponding array value will be the string "42". The note makes no comment on the array's keys.
PHP type comparison tables
Die folgenden Tabellen zeigen das Verhalten von Typen und Vergleichsoperatoren in PHP, sowohl für schwach und stark typisierte Vergleiche. Dieser Anhang steht im Zusammenhang mit dem Abschnitt zu Type juggling. Anregungen für diesen Abschnitt kamen aus verschiedenen Benuterkommentaren und der Arbeit von » BlueShoes.
Bevor diese Tabellen verwendet werden ist es wichtig die Typen und ihre
Bedeutungen zu verstehen. "42" ist zum Beispiel ein
string, während 42 ein integer
ist. FALSE ist ein boolean, obwohl
"false" ein string ist.
Hinweis:
HTML-Formulare übergeben keine integer, float oder boolesche Werte, sie übertragen strings. Um herauszufinden, ob ein string numerisch ist kann man is_numeric() verwenden.
Hinweis:
Wenn man einfach if ($x) verwendet, obwohl $x undefiniert ist, so wird ein Fehler der Stufe
E_NOTICEerzeugt. Verwenden sie stattdessen empty() oder isset() und/oder initialisiere sie die verwendeten Variablen.
Hinweis:
Einige numerische Operationen können ein Ergebnis erzeugen, welches durch die Konstante
NANrepresäntiert wird. Jeder schwach oder stark typisierte Vergleich dieses Wertes mit beliebigen anderen Werten, einschließlich der Konstante selbst, wirdFALSEergeben (d.h. NAN != NAN und NAN !== NAN). Beispiele für Operationen, dieNANergeben sind u.a. sqrt(-1), asin(2), und asinh(0).
| Ausdruck | gettype() | empty() | is_null() | isset() | boolean : if($x) |
|---|---|---|---|---|---|
| $x = ""; | string | TRUE |
FALSE |
TRUE |
FALSE |
| $x = null | NULL | TRUE |
TRUE |
FALSE |
FALSE |
| var $x; | NULL | TRUE |
TRUE |
FALSE |
FALSE |
| $x ist undefiniert | NULL | TRUE |
TRUE |
FALSE |
FALSE |
| $x = array(); | array | TRUE |
FALSE |
TRUE |
FALSE |
| $x = false; | boolean | TRUE |
FALSE |
TRUE |
FALSE |
| $x = true; | boolean | FALSE |
FALSE |
TRUE |
TRUE |
| $x = 1; | integer | FALSE |
FALSE |
TRUE |
TRUE |
| $x = 42; | integer | FALSE |
FALSE |
TRUE |
TRUE |
| $x = 0; | integer | TRUE |
FALSE |
TRUE |
FALSE |
| $x = -1; | integer | FALSE |
FALSE |
TRUE |
TRUE |
| $x = "1"; | string | FALSE |
FALSE |
TRUE |
TRUE |
| $x = "0"; | string | TRUE |
FALSE |
TRUE |
FALSE |
| $x = "-1"; | string | FALSE |
FALSE |
TRUE |
TRUE |
| $x = "php"; | string | FALSE |
FALSE |
TRUE |
TRUE |
| $x = "true"; | string | FALSE |
FALSE |
TRUE |
TRUE |
| $x = "false"; | string | FALSE |
FALSE |
TRUE |
TRUE |
TRUE |
FALSE |
1 | 0 | -1 | "1" | "0" | "-1" | NULL |
array() | "php" | "" | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
| 1 | TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| 0 | FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
TRUE |
TRUE |
| -1 | TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
| "1" | TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| "0" | FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| "-1" | TRUE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
NULL |
FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
TRUE |
| array() | FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
FALSE |
| "php" | TRUE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
| "" | FALSE |
TRUE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
TRUE |
TRUE |
FALSE |
1 | 0 | -1 | "1" | "0" | "-1" | NULL |
array() | "php" | "" | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
TRUE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| 1 | FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| 0 | FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| -1 | FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| "1" | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| "0" | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
| "-1" | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
FALSE |
NULL |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
FALSE |
| array() | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
FALSE |
| "php" | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
FALSE |
| "" | FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
FALSE |
TRUE |
PHP type comparison tables
23-Aug-2006 08:32
the manual said "HTML Forms do not pass integers, floats, or booleans; they pass strings"
while this is true, php will sometimes change the type to either type array, or possibly type integer(no, not a numeric string) if it was used as an array key. php seems to do this when it parses the request data into the predefined variable arrays.
example:
<input type="text" name="foo[5]">
<input type="text" name="foo[7]">
now obviously the browser will send those names as a string. but php will change thier type.
<?php
// $_POST['foo'] is an array
var_dump($_POST['foo']);
foreach ($_POST['foo'] as $key => $val) {
// the keys 5 and 7 will be type integer
var_dump($key);
}
?>
because of this, its also a good idea to check the types of your variables.
29-Dec-2005 08:23
Note that php comparison is not transitive:
"php" == 0 => true
0 == null => true
null == "php" => false
07-Sep-2005 09:44
In case it helps someone, here's a table to compare different Variable tests (created before I found this page!)
http://www.deformedweb.co.uk/php_variable_tests.php
26-Jul-2005 10:04
In some languages, a boolean is promoted to an integer (with a value of 1 or -1, typically) if used in an expression with an integer. I found that PHP has it both ways:
If you add a boolean with a value of true to an integer with a value of 3, the result will be 4 (because the boolean is cast as an integer).
On the other hand, if you test a boolean with a value of true for equality with an integer with a value of three, the result will be true (because the integer is cast as a boolean).
Surprisingly, at first glance, if you use either < or > as the comparison operator the result is always false (again, because the integer as cast as a boolean, and true is neither greater nor less than true).
17-Jun-2005 11:27
<?php
if (strlen($_POST['var']) > 0) {
// form value is ok
}
?>
When working with HTML forms this a good way to:
(A) let "0" post values through like select or radio values that correspond to array keys or checkbox booleans that would return FALSE with empty(), and;
(B) screen out $x = "" values, that would return TRUE with isset()!
Because HTML forms post values as strings, this is a good way to test variables!
[[Editor Note: This will create a PHP Error of level E_NOTICE if the checked variable (in this case $_POST['var']) is undefined. It may be used after (in conjuection with) isset() to prevent this.]]
24-Jan-2005 04:00
The way PHP handles comparisons when multiple types are concerned is quite confusing.
For example:
"php" == 0
This is true, because the string is casted interally to an integer. Any string (that does not start with a number), when casted to an integer, will be 0.