Таблица сравнения типов в PHP

Следующие таблицы демонстрируют работу PHP с типами переменных и операторами сравнения, как в случае свободного, так и в случае строгого сравнения. Также эта информация относится к разделу документации по приведению типов. Вдохновением на создание этого раздела мы обязаны различным комментариям пользователей и работе над » BlueShoes.

До осмотра таблиц, важно знать и понимать типы переменных и их значения. К примеру, "42" -- строка, в то время как 42 -- целое. FALSE -- логическое, а "false" -- строка.

Замечание: HTML-формы не передают тип переменной: они всегда передают строки. Для проверки является ли строка числом, используйте функцию is_numeric().

Замечание: Использование if ($x) пока $x не определена сгенерирует ошибку E_NOTICE. Вместо этого используйте функцию empty() или isset() и/или инициализируйте переменную.

Сравнение типов $x и результатов функций PHP, связанных с типами
Выражение gettype() empty() is_null() isset() логическое : if($x)
$x = ""; строка TRUE FALSE TRUE FALSE
$x = NULL NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x неопределена NULL TRUE TRUE FALSE FALSE
$x = array(); массив TRUE FALSE TRUE FALSE
$x = false; логическое TRUE FALSE TRUE FALSE
$x = true; логическое FALSE FALSE TRUE TRUE
$x = 1; целое FALSE FALSE TRUE TRUE
$x = 42; целое FALSE FALSE TRUE TRUE
$x = 0; целое TRUE FALSE TRUE FALSE
$x = -1; целое FALSE FALSE TRUE TRUE
$x = "1"; строка FALSE FALSE TRUE TRUE
$x = "0"; строка TRUE FALSE TRUE FALSE
$x = "-1"; строка FALSE FALSE TRUE TRUE
$x = "php"; строка FALSE FALSE TRUE TRUE
$x = "true"; строка FALSE FALSE TRUE TRUE
$x = "false"; строка 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 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 FALSE TRUE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE 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 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

Замечание: Заметка о PHP 3.0 Строка "0" считалась не пустой, в PHP4 ситуация изменилась: строка трактуется как пустая.

Коментарии

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.
2005-01-24 09:00:06
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
<?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.]]
2005-06-17 05:27:52
http://php5.kiev.ua/manual/ru/types.comparisons.html
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).
2005-07-26 16:04:32
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
Note that php comparison is not transitive:

"php" == 0 => true
0 == null => true
null == "php" => false
2005-12-29 13:23:21
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
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.
2006-08-23 14:32:17
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
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.
2007-03-15 16:06:25
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
A comparison table for <=,<,=>,> would be nice...
Following are TRUE (tested PHP4&5):
NULL <= -1
NULL <= 0
NULL <= 1
!(NULL >= -1)
NULL >= 0
!(NULL >= 1)
That was a surprise for me (and it is not like SQL, I would like to have the option to have SQL semantics with NULL...).
2007-08-14 18:06:35
http://php5.kiev.ua/manual/ru/types.comparisons.html
Compare object
<?php
$o 
= new stdClass();
$o->we 12;

$o2 = new stdClass();
$o2->we 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //true
?>
2008-01-06 15:51:14
http://php5.kiev.ua/manual/ru/types.comparisons.html
Universal comparison test.

<?php

$tests 
= array();
$tests['==']  = create_function('$a, $b''return $a==$b;');
$tests['==='] = create_function('$a, $b''return $a===$b;');
$tests['!='] = create_function('$a, $b''return $a!=$b;');
$tests['<>'] = create_function('$a, $b''return $a<>$b;');
$tests['!=='] = create_function('$a, $b''return $a!==$b;');
$tests['<']   = create_function('$a, $b''return $a<$b;');
$tests['>']   = create_function('$a, $b''return $a>$b;');
$tests['<=']  = create_function('$a, $b''return $a<=$b;');
$tests['>=']  = create_function('$a, $b''return $a>=$b;');

$comparison = array();
$comparison['TRUE'] = true;
$comparison['FALSE'] = false;
$comparison['1'] = 1;
$comparison['0'] = 0;
$comparison['-1'] = -1;
$comparison['3,14'] = pi();
$comparison['"1"'] = '1';
$comparison['"0"'] = '0';
$comparison['"-1"'] = '-1';
$comparison['NULL'] = null;
$comparison['array()'] = array();
$comparison['"php"'] = 'php';
       
print 
'<h1>PHP version '.PHP_VERSION.' type comparison tables</h1>';
       
foreach (
$tests as $test=>$function) {
    print 
"<h2>Comparisons with $test</h2>";
    print 
"<table border='1'>";
    print 
"<tr>";
    print 
"<th>&nbsp;</th>";
    foreach (
array_keys($comparison) as $name) {
        print 
"<th>$name</th>";
    }
    print 
"</tr>";
    foreach (
$comparison as $arg_1_name => $arg_1_value) {
        print 
'<tr>';
        print 
"<th>$arg_1_name</th>";
        foreach (
$comparison as $arg_2_value) {
            print 
'<td>';
            print 
$function($arg_1_value$arg_2_value)==true ?
                       
'<span style="color:#00F;">TRUE</span>' '<span style="color:#F00;">FALSE</span>';
            print 
'</td>';
        }
        print 
"</tr>";
    }
    print 
"</table>";
}

?>
2008-05-08 03:48:41
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
The note about object comparison should be corrected. Cloning objects does not imply instances are the same, so === would return FALSE. 

Compare object
<?php
$o 
= new stdClass();
$o->we 12;

$o2 = new stdClass();
$o2->we 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //false
?>
2008-05-08 10:20:34
http://php5.kiev.ua/manual/ru/types.comparisons.html
Some function to write out your own comparisson table in tsv format. Can be easily modified to add more testcases and/or binary functions. It will test all comparables against each other with all functions. 

<?php
$funcs 
= array(
       
/* Testing equality */
       
'eq' => '==',
       
'ne' => '!=',
       
'gt' => '>',
       
'lt' => '<',
       
'ne2' => '<>',
       
'lte' => '<=',
       
'gte' => '>=',
       
/* Testing identity */
       
'id' => '===',
       
'nid' => '!=='
);
class 
Test {
        protected 
$a;
        public 
$b;
        public function 
__construct($a,$b){
               
$this->$a;
               
$this->$b;
        }
        public function 
getab(){
                return 
$this->a.","$this->b;
        }

}
$tst1 = new Test(1,2);
$tst2 = new Test(1,2);
$tst3 = new Test(2,2);
$tst4 = new Test(1,1);

$arr1 = array(1,2,3);
$arr2 = array(2,3,4);
$arr3 = array('a','b','c','d');
$arr4 = array('a','b','c');
$arr5 = array();

$comp1 = array(
       
'ints' => array(-1,0,1,2),
       
'floats' => array(-1.1,0.0,1.1,2.0),
       
'string' => array('str''str1''''1'),
       
'bools' => array(truefalse),
       
'null' => array(null),
       
'objects' => array($tst1,$tst2,$tst3,$tst4),
       
'arrays' => array($arr1$arr2$arr3$arr4$arr5)
);
$fbody = array();

foreach(
$funcs as $name => $op){
       
$fbody[$name] = create_function('$a,$b''return $a ' $op ' $b;');
}

$table = array(array('function''comp1''comp2''f comp1 comp2''type'));
/* Do comparisons */
$comp2  = array();
foreach(
$comp1 as $type => $val){
       
$comp2[$type] = $val;
}

foreach(
$comp1 as $key1 => $val1){
        foreach(
$comp2 as $key2 => $val2){
               
addTableEntry($key1$key2$val1$val2);
        }
}
$out '';
foreach(
$table as $row){
       
$out .= sprintf("%-20s\t%-20s\t%-20s\t%-20s\t%-20s\n"$row[0], $row[1], $row[2], $row[3], $row[4]);
}

print 
$out;
exit;

function 
addTableEntry($n1$n2$comp1$comp2){
        global 
$table$fbody;
        foreach(
$fbody as $fname => $func){
                        foreach(
$comp1 as $val1){
  foreach(
$comp2 as $val2){
                                       
$val $func($val1,$val2);
                                               
$table[] = array($fnamegettype($val1) . ' => ' sprintval($val1), gettype($val2) .' => ' sprintval($val2), gettype($val) . ' => ' sprintval($val), gettype($val1) . "-" gettype($val2) . '-' $fname);
                                        }
                        }
        }
}

function 
sprintval($val){
        if(
is_object($val)){
                return 
'object-' $val->getab();
        }
        if(
is_array($val)){
                return 
implode(','$val);
        }
        if(
is_bool($val)){
                if(
$val){
                        return 
'true';
                }
                return 
'false';
        }
        return 
strval($val);
}

?>
2009-12-15 08:55:54
http://php5.kiev.ua/manual/ru/types.comparisons.html
It's interesting to note that 'empty()' and 'boolean : if($x)'
are paired as logical opposites, as are 'is_null()' and 'isset()'.
2010-02-15 12:31:09
http://php5.kiev.ua/manual/ru/types.comparisons.html
When we're doing loose comparisons (with double equals signs), the following fact is interesting.

false == array() evaluates to true
false == "" evaluates to true
array() == "" however, evaluates to false
2011-02-18 09:12:13
http://php5.kiev.ua/manual/ru/types.comparisons.html
Автор:
If $var not declared.

In php 5.2 :
<?php if($var?> - work

in php 5.3 :
<?php if($var?> - dont work and generate error E_NOTICE
2011-06-17 05:18:19
http://php5.kiev.ua/manual/ru/types.comparisons.html
Be wary of string-comparison where both strings might be interpreted as numbers.  Eg:

$x="123456789012345678901234567890"; $y="123456789012345678900000000000";
echo  ($x==$y)?"equal":"not_equal";   #Prints equal !!

Both strings are getting converted to floats, then losing precision, then becoming equal :-(

Using "===" or making either of the strings non-numeric will prevent this.
[This is on a 32-bit machine, on a 64-bit, you will have to make the strings longer to see the effect]
2012-04-12 22:45:28
http://php5.kiev.ua/manual/ru/types.comparisons.html

    Поддержать сайт на родительском проекте КГБ