Operators and expressions are the most basic knowledge in php learning. This article uses examples to explain some of the applications and skills of operators and expressions in php, as well as what you need to pay attention, for more information, see.
Php operators and expressions
I. classification of operators
1. classification by operands
1 .! True // unary operator
2. $ a + $ B // binary operator
3. true? 1:0 // ternary operator
2. classification by Operation function
(1) arithmetic operators
1. +,-, x,/, % (remainder)
(2) string operators
1. // for example: $ a = 'abc'. 'efg ';
(3) value assignment operator
1. = // simple assignment
2. + =,-=, X =,/=, % =,. = // composite value assignment
3. ++ ($ a ++, ++ $ a), -- ($ a --, -- $ a) // decrease progressively
4. & ($ a = 1; $ B = & a) // reference value assignment
(4) comparison operators
1. =, = (constant ),! = ,! ===, <> (Not equal to), <,>, <=,> =
(5) logical operators
1. // The priority inside the brackets is higher than that outside the brackets.
2. & (and), | (or ),! (Not), xor (exclusive or ),
(6) bitwise operators
1. & (by bit and), | (by bit OR ),~ (Not by bit), ^ (by bit or exclusive), <(left shift),> (right shift)
II. arithmetic operators
% Remainder, common usage: 1) division operation 2) control value range
Instance: determines whether it is a leap year (four years, one hour, one hundred years without renewal, four hundred years before renewal)
// % Converts the numbers on both sides into an integer and then returns the integer. // % digits cannot be fractional or negative if ($ year % 4 = 0) & ($ year % 100! = 0) | $ year % 400 = 0) echo ""; elseecho "";
III. assignment operators
// Add 10 first, and then assign the value to yourself, which is equivalent to $ a = $ a + 10 $ a + = 10. // The Increment and decrease of the front side, increment and decrease first, then assign a value + $ a // first, then increment and decrease $ a ++ // instance $ a = 10; $ B = $ a ++ $ c = -- $ B result: a = 11 B = 9 c = 9
IV. logical operators
Xor exclusive OR: The same is false (two true or two false = false), the difference is true (one true or one false = false)
Tips: pay attention to the difference with or, or two are true = true
Short circuit of logical operators
1. if one of them is false, no computation is performed. it must be false.
2. | // if the input value is true, no operation is performed next to it. it must be true.
Fopen ("test. php", "r") or die ("failed ");
Tips: Operator & | no short circuit
5. bitwise operators
Bitwise operation: convert an integer to a 32-bit binary string to an ANSCA code for processing.
Thank you for reading this article. I hope it will help you. thank you for your support for this site!