Summary of the use of operators in php

Source: Internet
Author: User
Tags bit set
Arithmetic operators 1. arithmetic operators: +,-, *, and %. 2. increment/decrease operators: for example, $ a ++, $ a --, ++ $ a, -- $. the instance code is as follows :? Php $ a10; $ bnbsp... arithmetic operator

1. arithmetic operators: +,-, *,/, and %.

2. increment/decrease operators: for example, $ a ++, $ a --, ++ $ a, -- $.

The instance code is as follows:

 
 

Ternary operators

(Expr1 )? (Expr2): (expr3 );

Explanation: If the condition "expr1" is true, execute the statement "expr2"; otherwise, execute "expr3 ".

The syntax of the following statements is correct. they ignore the second or third "yuan" in the form of small quotation marks ":

The instance code is as follows:

$a>$b ? print "Yes" : ""; $a>$b ? '': print 'No';


Note: We recommend that you use the print statement instead of the echo statement when using the ternary operator. Note the following statements:

The instance code is as follows:

$ Str = $ _ GET ['ABC']? 'Wangjinbo': 'wjb ';

This cannot be understood as: when $ str is equal to $ _ GET ['ABC'], the value is assigned to 'wangjinbo'; otherwise, the value is assigned to 'wjb '; because 1: to determine equality, use =; because the syntax of the original binary: ternary operator is as shown above: (expr1 )? (Expr2): (expr3). Obviously, the preceding binary, ternary 'wangjinbo' or 'wjb 'cannot constitute a meaningful expression separately. the correct understanding is: when $ _ GET ['ABC'] is null (whether or not, '', null, 0, undifine in PHP, all are equivalent to the boolean value false, assign $ str to 'wangjinbo'; otherwise, assign 'wjb ';

Logical operators:

The instance code is as follows:

 8 | $ B ++> 7) {// $ a ++> 8 is true, $ B ++> 7 does not execute echo 'OK! ';} Echo 'a ='. $ a. 'B ='. $ B; // output OK, a = 11, B = 7?>

Change

 10 & $ B ++> 7) {// $ a ++> 8 is false, and $ B ++> 7 does not execute echo 'OK! ';} Echo 'a ='. $ a. 'B ='. $ B; // a = 11, B = 7?>

Details: and & all indicate logic and where are their differences?

Mainly reflected in priority, and priority

The instance code is as follows:

And <= <&&

Or <= <|

$ A = false | true; // & >=> and; compare false first | true, and then assign a value.

$ B = false or true; // | |>=> or; first, assign $ B = false and then compare. Therefore, the result is false.

Var_dump ($ a, $ B); // bool (true) bool (false)

Bitwise operators

Displacement is a mathematical operation in PHP. the bits removed from any direction are discarded. when the left shift is performed, the right side is filled with zero. when the symbol bit is removed, the plus and minus signs are not retained. when the right shift is performed, the left side is filled with signs, indicating that the plus and minus signs are retained. use parentheses to ensure the desired priority. for example, if $ a & $ B = true, compare and then compare by bit, and ($ a & $ B) = true, compare by bit and then.

Note the data type conversion. if both the left and right parameters are strings, the bitwise operator operates the ASCII value of the characters.

PHP ini sets error_reporting to use the bitwise value, and provides a real example of disabling a bit. to display all the errors except the prompt level, php. ini uses this method:

E_ALL &~ E_NOTICE

The specific operation method is to first obtain the value of E_ALL:

00000000000000000111011111111111

Then get the value of E_NOTICE:

00000000000000000000000000001000

Then pass ~ Reverse:

11111111111111111111111111110111

Finally, we use the bitwise AND (&) to get the bit (1) set in both values:

00000000000000000111011111110111

Another method is to use bitwise or XOR (^) to obtain the bit set only in one of the values: E_ALL ^ E_NOTICE

Error_reporting can also be used to demonstrate how to set a location. only error and recoverable error can be displayed by: E_ERROR | E_RECOVERABLE_ERROR

That is, E_ERROR

00000000000000000000000000000001 and E_RECOVERABLE_ERROR

00000000000000000001000000000000

Use the bitwise OR (|) operator to obtain the result of being set in any value:

00000000000000000001000000000001

Example # The AND, OR, and xor operators of the integer 1

The instance code is as follows:

 

The above routine will output:

---------     ---------  -- ---------result        value      op test---------     ---------  -- ---------Bitwise AND( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101)( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101)( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101)( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101)( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101)Bitwise Inclusive OR( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)(13 = 1101) = ( 8 = 1000) | ( 5 = 0101)Bitwise Exclusive OR (XOR)( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)(13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)

Comparison Operators

If you compare a number with a string or a string that involves the content of a number, the string is converted to a value and compared to a value. this rule also applies to switch statements. when = or! = During comparison, no type conversion is performed because the types and values must be compared.

The instance code is as follows:

  truevar_dump("1" == "01"); // 1 == 1 -> truevar_dump("10" == "1e1"); // 10 == 10 -> truevar_dump(100 == "1e2"); // 100 == 100 -> trueswitch ("a") {    case 0:        echo "0";        break;    case "a": // never reached because "a" is already matched with 0        echo "a";        break;}?>

For multiple types, comparison operators are compared according to the following table (in order ).

Compare multiple types of operation numbers 1 type operation number 2 type result null or string convert NULL to "", compare the number or word bool or null any other type to bool, FALSE <TRUE object built-in class can define your own comparison, different classes cannot %


Address:

Reprinted at will, but please attach the article address :-)

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.