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
?>
PHP 자료형 비교표
다음 테이블은 PHP 자료형 과 느슨한 비교와 엄격한 비교를 위한 비교 연산자의 동작을 설명한다. 이 부록은 자료형 조절에 관한 매뉴얼 섹션과도 관련이 있다. 다양한 유저 주석과 » BlueShoes의 보고서를 통해 영감을 얻을수 있다.
이 테이블을 이용하기 전에, 자료형과 각 자료형의 의미를 이해해야 한다. 예를 들면, "42"는 string형이고, 반면에 42는 integer형이다. FALSE는 boolean형이고, "false"는 string형이 되는것이다.
Note: HTML 폼은 정수, 부동소수점, 논리형으로 전달하지 않는다. 문자열형으로 전달한다. 문자열이 숫자값인지 확인하기 위해서는 is_numeric()를 사용할수 있다.
Note: $x가 선언되지 않았는데 단순히 if ($x)를 수행하면 E_NOTICE 레벨의 에러가 발생할것이다. 대신 empty()나 isset()을 사용하고 그 변수를 초기화시키도록 한다.
| Expression | 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 is undefined | 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 자료형 비교표
rich
08-May-2008 11:20
08-May-2008 11:20
gernovich at ya dot ru
08-May-2008 04:48
08-May-2008 04:48
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> </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>";
}
?>
info at shaelf dot ru
07-Jan-2008 06:51
07-Jan-2008 06:51
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
?>
frank
15-Aug-2007 07:06
15-Aug-2007 07:06
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...).
16-Mar-2007 07:06
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.
omit
24-Aug-2006 03:32
24-Aug-2006 03: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.
Jan
30-Dec-2005 04:23
30-Dec-2005 04:23
Note that php comparison is not transitive:
"php" == 0 => true
0 == null => true
null == "php" => false
php [at] barryhunter [.] co [.] uk
08-Sep-2005 04:44
08-Sep-2005 04: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
jerryschwartz at comfortable dot com
27-Jul-2005 05:04
27-Jul-2005 05: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 06:27
17-Jun-2005 06: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.]]
aidan at php dot net
25-Jan-2005 12:00
25-Jan-2005 12: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.
