PHP learning operator and operator priority

Source: Internet
Author: User
Tags bitwise operators
How to learn about operator priority in php
Operator name result
$ A + $ B addition $ a and $ B's and
$ A-$ B subtraction $ a and $ B
$ A * $ B multiplication $ the product of a and $ B
$ A/$ B Division $ a divided by the operator of $ B
$ A % $ B modulo $ a divided by the remainder of $ B
Increment/decrease operator
Operator name result
+ + $ A add the value of $ a plus one, and then perform the operation
$ A ++ add the value of $ a first, and then add one
-- Minus $ a before $ a minus one, and then perform the operation
$ A -- minus the value of $ a before the operation, minus one
Instance:
Echo $ a = 5 + "5th"; // output: 10
Echo 10% 3; // output: 1
Echo 10 + $ a ++; // output: 20
Echo 5--- $ a; // output:-5
?>
Comparison Operators
Operator name result
$ A = $ B = TRUE, if $ a is equal to $ B
$ A ===$ B all equal to TRUE, if $ a is equal to $ B, and their types are the same
$! = $ B is not equal to TRUE. if $ a is not equal to $ B
$ A <> $ B is not equal to TRUE. if $ a is not equal to $ B
$! = $ B is not fully equal to TRUE. if $ a is not equal to $ B, or they are of different types
$ A <$ B is less than TRUE, if $ a is strictly less than $ B
$ A> $ B is greater than TRUE. if $ a is strict, $ B
$ A <= $ B is less than or equal to TRUE. if $ a is less than or equal to $ B
$ A >=$ B is greater than or equal to TRUE. if $ a is greater than or equal to $ B
Another conditional operator is "? : "(Or ternary) operator.

Instance:
Var_dump (0 = "a"); // output: bool (true)
Var_dump (0 = "00"); // output: bool (true)
Var_dump (0 = "00"); // output: bool (false)
Var_dump (0 <> "abc"); // output: bool (false)
Var_dump (0! = "01"); // output: bool (true)
$ A = 10;
$ B = 20;
$ Str = $ a> $ B? "True": "false ";
Echo $ str; // output: false
?>
Logical operators
Operator name result
$ A and $ B logic and TRUE, if both $ a and $ B are TRUE.
$ A or $ B logic or TRUE, if $ a or $ B is TRUE.
$ A xor $ B is different or TRUE. if $ a and $ B are different
! $ A logic is not TRUE, if $ a is not TRUE.
$ A & $ B logic and TRUE, if both $ a and $ B are TRUE.
$ A | $ B logic or TRUE, if $ a or $ B is TRUE.

And &, or and | are two methods of writing the same logical operator.
Both logic and logic are short-circuit operators. When the following logical expressions are encountered, the PHP interpreter does not calculate the expression on the right:
$ A = 10;
If (false & (++ $ ));
Echo $ a; // output: 10
$ B = 10;
If (true or (++ $ B ));
Echo $ B; // output: 10
?>
Bitwise operators
The bitwise operator allows you to set the specified bit in an integer. If both the left and right parameters are strings, bitwise operators operate on the ASCII value of the character.
Expression name result
$ A & $ B is set to 1 for both $ a and $ B.
$ A | $ B is set to 1 in bitwise OR $ a or $ B.
$ A ^ $ B is set to 1 in bitwise OR in the case of $ a and $ B.
~ $ A does not set the bitwise where $ a is 0 to 1, and vice versa.
$ A <$ B move left to move the bits in $ a to the left for $ B (each movement represents "multiplied by 2 ").
$ A >> B B shifts right the bits in $ a to the right $ B (each movement indicates "divided by 2 ").
Other operators
String operators
There are two string operators. The first is the concatenation operator ("."), which returns the string after the left and right parameters are connected. The second is the concatenation of the value assignment operator (". ="), which attaches the parameter on the right to the parameter on the left.
Error suppression operator
When the most common database connection and file creation operations or exceptions except 0 occur, you can use the @ symbol to suppress function error messages and output them to the browser side $ a =@ (5/0)
External command execution
Run the external system command using ''. Note that it is not a single quotation mark. it is the button under ESC.
$ Out = 'dir c :';
Print_r ($ out );
?> // Not recommended
Instance:
$ A = "hello ";
$ A. = "world! "; // Equivalent to: $ a = $ a." world! ";
Echo $ a; // output: hello world!
$ M = 3;
$ M + = 5; // equivalent to: $ m = $ m + 5;
Echo $ m; // output: 8

$ C = ($ B = 4) + 5;
Echo $ c; // output: 9
?>
Operator priority
The following table lists the operator priorities from low to high.
Combined direction operators
Left,
Left or
Left xor
Left and
Right print
Right = + =-= * =/=. = % = & = | = ^ = ~ = <=> =
Left? :
Left |
Left &&
Combined direction operators
Left |
Left ^
Left &
None =! ===! =
None <=> =
Left <>
Left + -.
Left */%
Right! ~ ++ -- (Int) (float) (string) (array) (object )@
Right [
No new

The above is the content of the PHP learning operator and operator priority. For more information, see PHP Chinese network (www.php1.cn )!

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.