It's interesting to note that 'empty()' and 'boolean : if($x)'
are paired as logical opposites, as are 'is_null()' and 'isset()'.Π‘Π»Π΅Π΄ΡΡΡΠΈΠ΅ ΡΠ°Π±Π»ΠΈΡΡ ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡ ΡΠ°Π±ΠΎΡΡ PHP Ρ ΡΠΈΠΏΠ°ΠΌΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠ°ΠΌΠΈ ΡΡΠ°Π²Π½Π΅Π½ΠΈΡ ΠΏΡΠΈ ΡΠ»Π°Π±ΡΡ ΠΈ ΡΡΡΠΎΠ³ΠΈΡ ΡΡΠ°Π²Π½Π΅Π½ΠΈΡΡ . ΠΡΠ° ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΡΠ°ΠΊΠΆΠ΅ ΠΎΡΠ½ΠΎΡΠΈΡΡΡ ΠΊ ΡΠ°Π·Π΄Π΅Π»Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠΎ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½ΠΈΡ ΡΠΈΠΏΠΎΠ². ΠΠ°ΠΏΠΈΡΠ°ΡΡ ΡΠ°Π·Π΄Π΅Π» ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ² PHP Π²ΠΎΠΎΠ΄ΡΡΠΈΠ²ΠΈΠ»ΠΈ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ ΠΈ ΡΠ°Π±ΠΎΡΠ° Π½Π°Π΄ ΡΡΠ΅ΠΉΠΌΠ²ΠΎΡΠΊΠΎΠΌ » BlueShoes.
ΠΠ΅ΡΠ΅Π΄ Π½Π°ΡΠ°Π»ΠΎΠΌ ΡΠ°Π±ΠΎΡΡ Ρ ΡΠ°Π±Π»ΠΈΡΠ°ΠΌΠΈ Π²ΡΠΏΠΎΠΌΠ½ΠΈΠΌ Π±Π°Π·ΠΎΠ²ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΠΈΠΏΠ°Ρ
.
ΠΠ°ΡΠΈΠΌΠ΅Ρ, "42" β ΡΡΡΠΎΠΊΠ° (string),
Π° 42 β ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ (int), false β
Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ (bool), Π° "false" β ΡΡΡΠΎΠΊΠ° (string).
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅:
HTML-ΡΠΎΡΠΌΡ Π½Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅, ΡΠ΅Π»ΡΠ΅ ΠΈΠ»ΠΈ Π΄ΡΠΎΠ±Π½ΡΠ΅ ΡΠΈΡΠ»Π°: ΠΈΠ· ΡΠΎΡΠΌ ΠΏΡΠΈΡ ΠΎΠ΄ΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΡΡΠΎΠΊΠΈ. Π€ΡΠ½ΠΊΡΠΈΡ is_numeric() ΠΏΡΠΎΠ²Π΅ΡΡΠ΅Ρ, ΠΎΡΠ½ΠΎΡΠΈΡΡΡ Π»ΠΈ ΡΡΡΠΎΠΊΠ° ΠΊ ΡΠΈΡΠ»ΠΎΠ²ΠΎΠΉ.
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅:
ΠΡΡΠ°ΠΆΠ΅Π½ΠΈΠ΅
if ($x), Π΅ΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ $x Π½Π΅ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΠ»ΠΈ, ΡΠ³Π΅Π½Π΅ΡΠΈΡΡΠ΅Ρ ΠΎΡΠΈΠ±ΠΊΡ ΡΡΠΎΠ²Π½ΡE_NOTICE, ΠΏΠΎΡΡΠΎΠΌΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΡΠ½Π°ΡΠ°Π»Π° ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΡΡ ΠΈΠ»ΠΈ Π² ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΡΠ»ΠΎΠ²ΠΈΠΈ ΠΏΡΠΎΠ²Π΅ΡΡΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΡΠ·ΡΠΊΠΎΠ²ΠΎΠΉ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΈΠ΅ΠΉ empty() ΠΈΠ»ΠΈ isset().
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅:
ΠΡΠ΄Π΅Π»ΡΠ½ΡΠ΅ Π°ΡΠΈΡΠΌΠ΅ΡΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΠ΅Ρ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ°
NAN(Π°Π½Π³Π». Not A Number, Π½Π΅ΡΠΈΡΠ»ΠΎ). Π‘ΡΡΠΎΠ³ΠΎΠ΅ ΠΈΠ»ΠΈ Π½Π΅ΡΡΡΠΎΠ³ΠΎΠ΅ ΡΡΠ°Π²Π½Π΅Π½ΠΈΠ΅ ΡΠ°ΠΊΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Ρ Π΄ΡΡΠ³ΠΈΠΌ, Π²ΠΊΠ»ΡΡΠ°Ρ Π΅Π³ΠΎ ΡΠ°ΠΌΠΎ, Π½ΠΎ ΠΈΡΠΊΠ»ΡΡΠ°Ρtrue, Π²Π΅ΡΠ½ΡΡfalse:NAN != NANΠΈNAN !== NAN. ΠΡΠΈΠΌΠ΅ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΡNAN:sqrt(-1),asin(2)ΠΈacosh(0).
| ΠΡΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ | gettype() | empty() | is_null() | isset() | bool : if($x) |
|---|---|---|---|---|---|
$x = ""; |
string | 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 = ['a', 'b']; |
array | false |
false |
true |
true |
$x = false; |
bool | true |
false |
true |
false |
$x = true; |
bool | false |
false |
true |
true |
$x = 1; |
int | false |
false |
true |
true |
$x = 42; |
int | false |
false |
true |
true |
$x = 0; |
int | true |
false |
true |
false |
$x = -1; |
int | 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 |
[] |
"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 |
false* |
false* |
-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 |
[] |
false |
true |
false |
false |
false |
false |
false |
false |
true |
true |
false |
false |
"php" |
true |
false |
false |
false* |
false |
false |
false |
false |
false |
false |
true |
false |
"" |
false |
true |
false |
false* |
false |
false |
false |
false |
true |
false |
false |
true |
true Π΄ΠΎ PHP 8.0.0.
It's interesting to note that 'empty()' and 'boolean : if($x)'
are paired as logical opposites, as are 'is_null()' and 'isset()'.Note that php comparison is not transitive:
"php" == 0 => true
0 == null => true
null == "php" => falseA 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...).The truth tables really ought to be colorized; they're very hard to read as they are right now (just big arrays of TRUE and FALSE).
Also, something to consider: clustering the values which compare similarly (like is done on qntm.org/equality) would make the table easier to read as well. (This can be done simply by hand by rearranging the order of headings to bring related values closer together).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 = $a;
$this->b = $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(true, false),
'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($fname, gettype($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);
}
?>Be aware of the difference between checking the *value* of an array item, and checking the *existence* of an array item:
<?php
$arr = [
'x' => 0,
'y' => null,
];
isset($arr['x']); // true, same as isset(0)
isset($arr['y']); // false, same as isset(null)
array_key_exists('y', $arr); // true, though the value is null
array_key_exists('z', $arr); // falseIn 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).There is also 0.0 which is not identical to 0.
$x = 0.0;
gettype($x); // double
empty($x); // true
is_null($x); //false
isset($x); // true
is_numeric($x); // true
$x ? true : false; // false
$x == 0; // true
$x == "0"; // true
$x == "0.0"; // true
$x == false; // true
$x == null; // true
$x === 0; // false
$x === false; // false
$x === null; // false
$x === "0"; // false
$x === "0.0"; // false