Things to note about PHP comparison and logic operations

Source: Internet
Author: User
Tags scalar
1. Logical operations always return TRUE or false (note to people who write more JavaScript), logical operator precedence from high to low for &&, | |, and, or, short-circuit effects of logical operators can be used in statements, but remember they don't look 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

2. Switch comparison is not "= = =" but "= =" (in javascript "= = =")

3. unassigned variable, undeclared variable, 0, "0", "", false, NULL, empty array (), object's Magic Method get () return 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

4. The value that is judged false by Isset (): The value returned by an unassigned variable, an undeclared variable, NULL, get (), and the accepted participation in empty (), cannot be a constant and an expression.

5. In PhP4, the object is compared in the same way as the array, and in php5, the "= =" Comparison between the object types is true if they belong to an instance of the same class (and, of course, a comparison of attributes, which is similar to a scalar "= = =" comparison). the "= = =" Comparison between object 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 index is rounded down to 0,php5.4 after not forensics, is determined to be trueempty ($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}

6. 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

7. 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 get () is determined by a specific value)

6. Arrays can be directly 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)

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). ' Value: '. $value;}

Output Result:

ba,length:4 value:private property, $A of class Baa,length:2 Value:public Propert Y, $AA of Class B*b,length:4 value:protected property, $B of the class Baa,length:4 Value:private property, $A of Class A * C,length:4 value:protected Property, $C of class A 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.