Summary of the use of operators in php

Source: Internet
Author: User
Tags arithmetic arithmetic operators bit set bitwise bitwise operators comparison logical operators

Arithmetic operators

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

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

For example:

The code is as follows: Copy code

<? Php
$ A = 10;
$ B = 5;
$ C = $ a ++; // assign values first and then auto-increment. $ C = $ a, $ a = $ a + 1
$ D = $ B --; // assign values first, and then subtract from them. $ D = $ B, $ B = $ A-1
Echo '$ a = '. $. "| ". '$ c = '. $ c. '<br/>'; // $ a = 11, $ c = 10
Echo '$ B = '. $ B. "| ". '$ d = '. $ d. '<br/>'; // $ B = 4, $ d = 5
?>

<? Php
$ A = 10;
$ B = 5;
$ C = ++ $ a; // auto-increment first, and then assign a value. $ A = $ a + 1, $ c = $
$ D = -- $ B; // first auto-subtract and then assign a value. $ B = $ A-1, $ d = $ B
Echo '$ a = '. $. "| ". '$ c = '. $ c. '<br/>'; // $ a = 11, $ c = 11
Echo '$ B = '. $ B. "| ". '$ d = '. $ d. '<br/>'; // $ B = 4, $ d = 4
?>

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 "RMB" in the form of small quotation marks ":

The code is as follows: Copy code
$ 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 code is as follows: Copy code

$ 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;

Correct understanding: when $ _ GET ['ABC'] is null (that is, whether, in PHP, '', null, 0, undifine, are equivalent to Boolean values false, assign $ str to 'wangjinbo'; otherwise, assign 'wjb ';

Logical operators:

For example:

The code is as follows: Copy code

$ A = 10; $ B = 7;
If ($ a ++> 8 | $ B ++> 7) {// $ a ++> 8 is true, $ B ++> 7 is not executed.
Echo 'OK! ';
}
Echo 'a = '. $ a.' B = '. $ B; // output OK, a = 11, B = 7

Change

$ A = 10; $ B = 7;
If ($ a ++> 10 & $ B ++> 7) {// $ a ++> 8 is false, $ B ++> 7 is not executed.
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 code is as follows: Copy code
And <= <&&
Or <= <|
For example:
$ 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. 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, which means the plus and minus signs are retained.

Use parentheses to ensure the desired priority. For example, if $ a & $ B = true, compare the values first and then compare them by bit. If ($ a & $ B) = true, compare the values by bit and then.

Note the data type conversion. If both the left and right parameters are strings, bitwise operators operate on the ASCII values of characters.

PHP ini sets error_reporting to use a bitwise value,
Provides a real example of disabling a bit. In addition to the prompt level
All other errors are used in php. ini as follows:
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 in one of the values:
E_ALL ^ E_NOTICE
     

Error_reporting can also be used to demonstrate how to set a location. Show only errors and recoverability
The error method is:
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 code is as follows: Copy code

<? Php
/*
* Ignore the top section,
* It is just formatting to make output clearer.
*/

$ Format = '(% 1 $ 2d = % 1 $ 04b) = (% 2 $ 2d = % 2 $ 04b )'
. '% 3 $ s (% 4 $ 2d = % 4 $ 04b)'. "n ";

Echo <EOH
-----------------------------
Result value op test
-----------------------------
EOH;


/*
* Here are the examples.
*/

$ Values = array (0, 1, 2, 4, 8 );
$ Test = 1 + 4;

Echo "n Bitwise AND n ";
Foreach ($ values as $ value ){
$ Result = $ value & $ test;
Printf ($ format, $ result, $ value, '&', $ test );
}

Echo "n Bitwise random sive OR n ";
Foreach ($ values as $ value ){
$ Result = $ value | $ test;
Printf ($ format, $ result, $ value, '|', $ test );
}

Echo "n Bitwise Exclusive OR (XOR) n ";
Foreach ($ values as $ value ){
$ Result = $ value ^ $ test;
Printf ($ format, $ result, $ value, '^', $ test );
}
?>
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 aggressive 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 code is as follows: Copy code

<? Php
Var_dump (0 = "a"); // 0 = 0-> true
Var_dump ("1" = "01"); // 1 = 1-> true
Var_dump ("10" = "1e1"); // 10 = 10-> true
Var_dump (100 = "1e2"); // 100 = 100-> true

Switch (""){
Case 0:
Echo "0 ";
Break;
Case "a": // never reached because "a" is already matched with 0
Echo "";
Break;
}
?>

 

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

Compare multiple types
Operation Number 1 type Operation count 2 type Result
Null or string String SetNULLConvert to "" for comparison of numbers or words
Bool or null Any other type Convert to bool,FALSE < TRUE
Object Object Built-in classes can define their own comparisons, and different classes cannot %

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.