This article mainly introduces the PHP comparison operation and the logical operation example detailed, the interest friend reference, hoped to be helpful to everybody.
1. The following values are judged to be true with empty ():
Unassigned variable, undeclared variable, 0, "0", "", false, NULL, empty array (), object's Magic Method __get () returns the value
In versions below PHP5.0, objects that do not have any attributes are also judged as true by empty
Note: empty () accepts only the index or attribute value of a variable or variable, cannot pass in a constant, or pass in an expression, and after PHP 5.5 supports an expression
2. The value that is isset () is evaluated as false: The value returned by the unassigned variable, the undeclared variable, NULL, __get (), and the accepted participation in empty (), cannot be a constant and an expression.
3. Comparison of different types of data
If there is a Boolean or null, converted to a Boolean comparison,
Otherwise if there is a number, converted to number comparison,
Otherwise, if there is a string, convert to a string comparison
The object type is always greater than the array type and scalar type, and the array type is always greater than the scalar type
Note the results of these comparisons:
0 The number of the beginning of the string to the number does not press the octal conversion, but simply discard the ' 0 ' discard by the number comparison, 123== ' 0123 '//true "123" < "0124"//true, 0 numeric string starting with a decimal number instead of octal "012" = =//false==//true 0x12 = =//True "0x12" = = +//True false & Lt True True 2>true; False 2==true; True null==0;//true-1<0;//true-1<null;//false,-1 turn bool is true
4. Type conversion rules
The value that is judged to be true by empty () is converted to Boolean to get false, and conversely, to get true (the value returned by __get () must be judged by a specific value)
The value that is judged as true by empty () is converted to number 0, and non-empty array-to-number gets 1 (the value returned by the __get () must be judged by a specific value)
Class test{private $k =1; public function __get ($propertyName) { return 123;}} $obj = new Test (); Echo Json_encode (Empty ($obj->k)); Trueecho Json_encode (isset ($obj->k)); Falseecho Json_encode ((bool) ($obj->k)); True
Several string-to-number scenarios:
Echo ' ABC ' * *; 0 echo ' 012 ' *; 12 Multiplication: Hexadecimal number can be converted, not the beginning of the number to return 0echo ' 0x12.123 ' *; 18echo (float) ' 0x12 ';//0 echo (int) ' 0x12 '; 0 cannot handle hexadecimal number echo (float) ' 12abc '; 12 intercept the Left string echo (float) ' abc '; 0 is not a number return 0is_numeric (' 0x123 '); True to recognize hexadecimal number is_numeric (' 0x123.123 '); False to recognize that the target is the entire string rather than the previous part
String goes to number when it intercepts the left-hand numeric string for conversion, and returns 0 if none.
Other data-to-string:
The value of several spin strings (string) 0; "0" (string) true; "1" (string) false; "" (string) null; "" (string) array (); "Array"
Arrays can be used for string concatenation but not for mathematical operations.
The object type is converted to Boolean always true, and the object type cannot be converted to number and string, so string concatenation and mathematical operations are not possible.
The way a scalar is converted to an array is to set the first element of the array to a scalar and return the array.
The scalar is converted to object to get an instance of the StdClass class, and the scalar value is assigned to the attribute named Scalar: object ([scalar] + 234)
Array to object gets an instance of the StdClass class, and the array key is the property name of the strength.
The object to array is somewhat complex:
Methods, static properties, class constants are discarded
The Protection property name is preceded by a "*"
The private attribute is preceded by the class name prefix (the case is exactly the same as the class names)
The prefix is preceded and followed by a null character,
For example, an array converted by object is:
Array ([*v] = 444 [BF] = 333 [BK] = 99977 [Ak] = 999 [*P] = 888 [A2] + 22)
The original objects are:
Public property A2, protected Property V, p, from which class cannot be authenticated (overridden to take the properties of the child class)
From class B's Private property F, K, (from the array key, in the case of BF, can not determine whether he is a property named Bf, or from Class B private property F)
Private attribute K from Class A
It is not possible to identify B and a which is the subclass which is the parent class (only from the key of the array and cannot infer which class the original object was constructed from)
Example:
Class A {private $A = ' private ', $A of Class A ';//This would become ' \0a\0a ' protected $C = ' protected property, $C of Class A ';} Class B extends A {private $A = ' private ', $A of Class B ';//This would become ' \0b\0a ' public $AA = ' public prop Erty, $AA of Class B '; This would become ' AA ' protected $B = ' protected property, $B of Class B ';} $arr = (array) new B (), foreach ($arr as $key = = $value) {echo ' <br/> '; Echo $key. ', Length: '. strlen ($key). ' VA Lue: '. $value;}
Output Result:
Ba,length:4 Value:private Property, $A the class Baa,length:2 Value:public property, $AA of class B*b,length:4 value: Protected, $B of Class Baa,length:4 Value:private property, $A of class A*c,length:4 value:protected property , $C of Class A
5, the logical operation always returns TRUE or false (the person who writes more JavaScript should note), the logical operator precedence from high to low for &&, | |, and, or, the short-circuit effect of the logical operator can be used in the statement, but remember that they do not like JAVASCR In IPT, returns a value that is not of type Boolean and is used in an expression to be noted.
$a = 1; $b =0; $b and $a = 100;echo $a; 1$b | | $a = 200;echo $a; 200
6, switch comparison is not "= = =" but "= =" (in JavaScript is "= = =")
7, in PhP4, the comparison between object is the same as the array, in php5, the "= =" between the object type is true if they belong to an instance of the same class (and, of course, a comparison of attributes, which is similar to the scalar "= = =" comparison), Obje the "= = =" Comparison between CT is true if they are the same object.
In PHP4, objects that do not include any member variable are judged by empty () to True
Empty () of the string offset offset character is judged by the character that corresponds to offset, and before PHP5.4, the index is rounded when a character is taken from a string using an index, so the string with no number on the left is converted to 0. After PHP5.4, the non-shaping string index is no longer rounded, so it is determined to be true, and isset () is determined to be false. Such as:
$str = ' ab0d '; empty ($str [0]); Falseempty ($str [0.5]); False index is rounded down to 0empty ($str ["0.5"]); False if the index is rounded down to 0,php5.4 and no forensics is found, it is determined to be true empty ($str [2]); True, the character obtained is "0" Empty ($str ["3"]); False, the character obtained is "D" Empty ($str [4]); True, the index is out of range, notice warning, but empty () ignores warning empty ($str [' a ']); False, the left side does not contain a numeric string index PHP5.4 before it is processed as $str [0],php5.4, directly to determine true
Do not use "transitivity" in PHP cross-type data comparisons, whether it is "not equal to" or "= =":
$a = = $b; True
$b = = $c; True
does not indicate $a = = $c is True
How to compare arrays
The array is a function standard_array_compare ($op 1, $op 2) {if (count ($op 1) < count ($op 2)) as compared with the standard comparison operator { return-1;//$op 1 < $op 2} elseif (count ($op 1) > Count ($op 2)) { return 1;//$OP 1 > $op 2} foreach ($op 1 as $key + = $val) { if (!array_key_exists ($key, $op 2)) { return null;//Uncomparable } elseif ($val < $op 2[$key]) { ret urn-1; } ElseIf ($val > $op 2[$key]) { return 1; }} return 0;//$op 1 = = $op 2}
8. Ternary operators: Unlike most other programming languages, PHP's ternary operators are left-associative!
$arg = ' T '; $vehicle = ($arg = = ' B ')? ' Bus ': ($arg = = ' A ')? ' Airplane ': ($arg = = ' T ')? ' Train ': ($arg = = ' C ')? ' Car ': ($arg = = ' H ')? ' Horse ': ' feet '); Echo $vehicle; Horse
Ternary operation expressions are divided into
($arg = = ' B ')? ' Bus ': ($arg = = ' A ') ? ' Airplane ': ($arg = = ' T ') ? ' Train ': ($arg = = ' C ') ? ' Car ': ($arg = = ' H ') ? ' Horse ': ' feet ';
Summary: The above is the entire content of this article, I hope to be able to help you learn.