|
|
 |
Ap茅ndice P. Tablas de comparaci贸n de tipos PHP
Las siguientes tablas demuestran los comportamientos de los tipos en PHP y los operadores de
comparaci贸n, tanto para comparaciones flexibles como
estrictas. Este suplemento se encuentra relacionado tambi茅n
con la secci贸n del manual sobre manipulaci贸n de
tipos. La inspiraci贸n ha provenido de varios
comentarios de usuarios, y del trabajo realizado en
BlueShoes.
Antes de usar estas tablas, es importante entender los tipos y sus
significados. Por ejemplo, "42"
es string mientras que 42 es
un integer. FALSE es un boolean
mientras "false" es string.
Nota:
Los Formularios HTML no pasan enteros, reales, o valores booleanos;
ellos pasan cadenas. Para saber si una cadena es num茅rica,
usted puede usar is_numeric().
Nota:
Hacer un simple if ($x)
cuando $x no est茅 definido,
generar谩 un error de nivel E_NOTICE. En
lugar de esto, considere el uso de empty()
o isset(), o inicializar sus variables.
Tabla P-1. Comparaciones de $x con funciones
PHP
Tabla P-2. Comparaciones flexibles con == | | 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 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | | 1 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | | 0 | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | | -1 | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | | "1" | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | | "0" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | | "-1" | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | | NULL | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | | array() | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | | "php" | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |
Tabla P-3. Comparaciones estrictas con === | | 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 | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | | 1 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | | 0 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | | -1 | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | | "1" | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | | "0" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | | "-1" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | | NULL | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | | array() | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | | "php" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |
Nota de PHP 3.0:
El valor de cadena "0" fue considerado como
no-vac铆o en PHP 3, este comportamiento cambi贸 en PHP
4 en donde es visto ahora como vac铆o.
add a note
User Contributed Notes
Tablas de comparaci贸n de tipos PHP
omit
23-Aug-2006 11: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
var_dump($_POST['foo']);
foreach ($_POST['foo'] as $key => $val) {
var_dump($key);
}
?>
because of this, its also a good idea to check the types of your variables.
Jan
29-Dec-2005 11:23
Note that php comparison is not transitive:
"php" == 0 => true
0 == null => true
null == "php" => false
jerryschwartz at comfortable dot com
26-Jul-2005 01: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).
tom
17-Jun-2005 02:27
<?php
if (strlen($_POST['var']) > 0) {
}
?>
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.]]
aidan at php dot net
24-Jan-2005 07: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.
| |
|
| Chiste de... Mam, mam | | Picaduras | Una monja a otra monja:
- Mira como me dejó la abispa.
Y la monja se levanta la falda y dice:
- pues mira como me dejó a mí el obispo. | | Chistes en tu mail | | ©ContenidosGratis |
| Inicio | Acción | Estrategia | Palabras | Puzzles | Solitarios | Foro Trucos |  | Cake Mania Jugadores: 6835 Categoría del juego: Acción Objetivo del juego: Ayuda a Jill a recuperar la pasteler韆 de su abuela llevando su propia pasteler韆; consigue clientes y gana dinero. |
|  | Rainbow Web Jugadores: 2199 Categoría del juego: Puzzles Objetivo del juego: Rompe un pegajoso hechizo y salva un reino de fantas韆 en Rainbow Web. Tendr醩 toneladas de diversi髇 mientras juegas a este m醙ico desaf韔 para la mente. |
|  | Mahjongg Fortuna Jugadores: 12462 Categoría del juego: Solitarios Objetivo del juego: Velocidad y habilidad mental son las armas más importantes en esta versión de un antiguo juego asiático. Despeja el tablero lo antes posible haciendo clic en las fichas iguales y gánate la fama eterna de la puntuación más alta. |
|  | Chainz 2 Jugadores: 6955 Categoría del juego: Puzzles Objetivo del juego: Entra en el mundo de las combinaciones con Chainz 2: Relinked, emocionante secuela del exitazo del a駉 pasado, Chainz. Gira eslabones y crea combinaciones de 3 m醩. |
|  | Delicious Jugadores: 4405 Categoría del juego: Acción Objetivo del juego: 縀res un as de la multitarea? 縌uieres que tus clientes est閚 contentos? ues Delicious es tu juego! Sacia el apetito de los clientes y tenlos contentos; o te arriesgues! |
|  | Bookworm Jugadores: 4568 Categoría del juego: Palabras Objetivo del juego: Junta las letras para formar palabras. ¡Las palabras más largas valen más puntos! |
|  | Zuma Jugadores: 4976 Categoría del juego: Acción Objetivo del juego: Controla el 韉olo de la rana de piedra de los antiguos Zuma en este intrigante enigma de acci髇. ispara bolas para formar conjuntos de tres, pero si dejas que lleguen a la calavera dorada morir醩! |
|  | Jewel of Atlantis Jugadores: 3798 Categoría del juego: Puzzles Objetivo del juego: Descubre la ciudad hundida de la Atl醤tida y busca valiosos tesoros. Viaja m醩 all de las profundidades del mar y vive trepidantes aventuras en Jewel of Atlantis. |
|  | Jewel Quest Jugadores: 3727 Categoría del juego: Puzzles Objetivo del juego: Convierte la arena de la antigua selva en oro tan r醦ido como puedas juntando grupos de 3 elementos. os grupos m醩 grandes valen m醩 puntos! |
|  | Bejeweled 2 Jugadores: 3659 Categoría del juego: Puzzles Objetivo del juego: Con cuatro modos de juego 鷑icos y fascinantes, nuevas piezas de juego explosivas e imponentes fondos planetarios, Bejeweled 2 es mucho m醩 adictivo que nunca. |
|
|