PHP comparison and logic operations, php logic operations _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP comparison and logic operations, php logic operations. PHP comparison and logic operations are described in detail. php logic operations are described in detail. 1. the following values are determined to be true using empty: non-assigned variables, non-declared variables, 0, 0, false, null, null PHP comparison and logic operations details, php logic operations details

1. the following values are determined to be true using empty:

Values returned from unassigned variables, undeclared variables, 0, "0", "", false, null, empty array (), and magic method _ get () of the object

In versions earlier than PHP5.0, empty determines that objects without any attributes are true.

Note: empty () only accepts the index value or attribute value of the variable or variable. it cannot directly input constants or computation expressions. it is supported after PHP 5.5.

2. values judged as false by isset (): values returned by unassigned variables, undeclared variables, null, and _ get (), which are the same as those accepted for empty, it cannot be a constant or expression.

3. Comparison of different types of data

If one of them is boolean or null, convert them to boolean for comparison,

Otherwise, if one is a number, convert it to a number comparison,

Otherwise, if one is string, convert it to string for comparison.

The object type is always greater than the array type and the scalar type, and the array type is always greater than the scalar type.

Note the following comparison results:

// When a numeric string starting with 0 is converted to a number, it is not converted to an octal value, but simply discarded and '0' is discarded and compared by a number, 123 = '000000' // true "0123" <"123" // true, A numeric string starting with 0 is directly compared with a decimal number, instead of an octal digit "012" = 10 // false = 10 // true 0x12 = 18 // true "0x12 "= 18 // true false <true; // true 2> true; // false 2 = true; // true null = 0; // true-1 <0; // true-1
 
  

4. type conversion rules

Convert the value determined by empty () to true to boolean to false. Otherwise, the value returned by true (_ get () must be determined based on the specific value)

The value determined by empty () to true is converted to number 0, and the non-empty array is converted to number to get the value returned by 1 (_ get () based on the 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 cases of string-to-number conversion:

Echo 'ABC' * 1; // 0 echo '012' * 1; // 12 multiplication: the hexadecimal number can be converted. if it is not the beginning of a number, 0 echo '0x12 is returned. 123 '* 1; // 18 echo (float) '0x12'; // 0 echo (int) '0x12'; // 0 cannot process hexadecimal echo (float) '12abc'; // 12 capture the left string echo (float) 'abc'; // 0 is not a number and returns 0is_numeric ('0x123 '); // true can recognize the hexadecimal number is_numeric ('0x123. 123 '); // false: identifies the entire string rather than intercepting the previous part.

When string is converted to number, the string on the left is truncated for conversion. If no value is displayed, 0 is returned.

Convert other data to strings:

// Several string conversion values (string) 0; // "0" (string) true; // "1" (string) false; // "" (string) null; // "" (string) array (); // "Array"

Arrays can be directly concatenated with strings, but cannot be computed in mathematics.

It is always true to convert the object type to boolean. the object type cannot be converted to number or string. Therefore, string concatenation and mathematical operations cannot be performed.

To convert a scalar value to an array, you can set the first element of the array to the scalar value. this array is returned.

Convert a scalar to an object to get an instance of the stdClass class. the scalar value is assigned to the attribute named scalar: Object ([scalar] => 234)

Convert array to object to get an instance of the stdClass class. the key of the array is the name of the Strength attribute.

Converting object to array is a bit complicated:

Methods, static attributes, and class constants are discarded.

"*" Is added before the protection property name "*"

The class name is prefixed with a private attribute (The case is the same as the class name)

Both the prefix and the prefix contain null characters \ 0.

For example, an array converted from an object is:

Array ([* v] => 444 [bf] => 333 [bk] => 99977 [Ak] => 999 [* p] => 888 [a2] => 22)

The original objects include:

Public attribute a2, protected attribute v, p, which cannot be identified by the class (the attribute of the subclass is obtained if it is overwritten)

Private attributes f and k from Class B (from the array key, taking bf as an example, it cannot be determined whether it is a property name bf or a private attribute f from class B)

Private attribute k from Class

It is impossible to identify which of the child classes of B and A and which is the parent class (only from the array key, it cannot be inferred from which class the original object is constructed)

Example:

class A { private $A = 'private property, $A of class A'; // This will become '\0A\0A' protected $C = 'protected property, $C of class A';}class B extends A { private $A = 'private property, $A of class B'; // This will become '\0B\0A' public $AA = 'public property, $AA of class B'; // This will become 'AA' protected $B = 'protected property, $B of class B';}$arr = (array) new B();foreach ($arr as $key => $value) { echo '
'; echo $key .',length: '.strlen($key).' value: '.$value;}

Output result:

BA,length: 4 value: private property, $A of class BAA,length: 2 value: public property, $AA of class B*B,length: 4 value: protected property, $B of class BAA,length: 4 value: private property, $A of class A*C,length: 4 value: protected property, $C of class A

5. logical operations always return true or false (attention should be paid to those who write more javascript). the priority of logical operators is from high to low: &, |, and, or, the short-circuit effect of logical operators can be used in the statement, but remember that they do not return a value not of the boolean type as in javascript. be careful when using the expression.

$a = 1;$b=0;$b and $a = 100;echo $a; //1$b || $a = 200;echo $a; //200

6. the comparison of the switch is not "=", but "=" (in javascript, it is "= ")

7. in php4, objects are compared in the same way as arrays. in php5, before "=" is true, they belong to instances of the same class (of course, attribute comparison is also required, which is similar to the comparison of scalar "= ), the premise that "=" between objects is true is that they are the same object.

In PHP4, objects that do not include any member variables are determined as true by empty ().

Empty () determination of string offset to take characters: take the corresponding offset character for judgment. before PHP5.4, the index is first rounded up when using the index to take characters from the string, therefore, strings that do not contain numbers on the left are converted to 0. after PHP5.4, the string indexes in non-integer format are no longer rounded up. Therefore, the result is true. Likewise, isset () false. for example:

$ Str = 'ab0d'; empty ($ str [0]); // falseempty ($ str [0.5]); // false indexes are rounded down to 0 empty ($ str ["0.5"]); // false indexes are rounded down to 0, and no evidence is obtained after PHP5.4, true empty ($ str [2]); // true, the obtained character is "0" empty ($ str ["3"]); // false, the obtained character is "d" empty ($ str [4]); // true, index out of range, notice warning, but empty () the warning empty ($ str ['A']) is ignored; // false, which is processed as $ str [0] and after PHP5.4 before the left side does not contain the numeric string index PHP5.4, determine true directly

No matter whether it is "not equal" or "=", do not use "passed" in PHP cross-type data comparison:

$ A = $ B; // true

$ B = $ c; // true

$ A = $ c is true.

Array comparison method

// The array is a function standard_array_compare ($ op1, $ op2) {if (count ($ op1) <count ($ op2) {return-1; // $ op1 <$ op2} elseif (count ($ op1)> count ($ op2) {return 1; // $ op1> $ op2} foreach ($ op1 as $ key => $ val) {if (! Array_key_exists ($ key, $ op2) {return null; // uncomparable} elseif ($ val <$ op2 [$ key]) {return-1 ;} elseif ($ val> $ op2 [$ key]) {return 1 ;}} return 0; // $ op1 ==$ op2}

8. ternary operators: Unlike most other programming languages, PHP's ternary operators are left-aligned!

$arg = 'T';  $vehicle = ( ( $arg == 'B' ) ? 'bus' :      ( $arg == 'A' ) ? 'airplane' :      ( $arg == 'T' ) ? 'train' :      ( $arg == 'C' ) ? 'car' :      ( $arg == 'H' ) ? 'horse' :      'feet' );  echo $vehicle; //horse

The ternary expression is divided

( $arg == 'B' ) ? 'bus' : ( $arg == 'A' )          ? 'airplane' : ( $arg == 'T' )                 ? 'train' : ( $arg == 'C' )                    ? 'car' : ( $arg == 'H' )                         ? 'horse' : 'feet' ;

The above comparison and logic operations in PHP are all the content that I have shared with you. I hope you can give us a reference and support the help house.

Http://www.bkjia.com/PHPjc/1125868.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125868.htmlTechArticlePHP comparison and logic operation details, php logic operation details 1, the following values with empty () is judged to be true: unassigned variables, undeclared variables, 0, "0", "", false, null, null...

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.