Arithmetic Operators
1, arithmetic operators: + 、-、 *,/,%.
2, Increment/decrement operators: such as $a++, $a--, + + $a,--$a.
Such as:
| The code is as follows |
Copy Code |
$a = 10; $b = 5; $c = $a + +; The value is first assigned and then self-increment. $c = $a, $a = $a +1 $d = $b--; The value is first assigned and then reduced. $d = $b, $b = $a-1 echo ' $a = '. $a. ' | | '. ' $c = '. $c. ' '; $a =11, $c =10 echo ' $b = '. $b. ' | | '. ' $d = '. $d. ' '; $b =4, $d =5 ?> $a = 10; $b = 5; $c =++ $a; Increment first, then assign value. $a = $a +1, $c = $a $d =--$b; The first self-subtraction, then assigns the value. $b = $a-1, $d = $b echo ' $a = '. $a. ' | | '. ' $c = '. $c. ' '; $a =11, $c =11 echo ' $b = '. $b. ' | | '. ' $d = '. $d. ' '; $b =4, $d =4 ?> |
Ternary operators
(EXPR1)? (EXPR2): (EXPR3);
Explanation: If the condition "Expr1" is true, the statement "EXPR2" is executed, otherwise "EXPR3" is executed.
The syntax for the following statements is correct, and they omit the second or third "meta" in pee quotes:
| The code is as follows |
Copy Code |
$a > $b? Print "Yes": ""; $a > $b? ": print ' No ';
|
It should be noted that the use of the print statement instead of the Echo statement is recommended when using the ternary operator.
Note the understanding of the following sequence of statements:
| The code is as follows |
Copy Code |
$STR = $_get[' abc ')? ' Wangjinbo ': ' WJB '; |
This cannot be understood as: when $str equals $_get[' abc ', the assignment is ' Wangjinbo ' otherwise assigned to ' WJB '; because one: judgment equality should be used = =; Because the original two: The syntax of the ternary operator is as shown above: (EXPR1)? (EXPR2): (EXPR3), apparently above the two yuan, ternary ' Wangjinbo ' or ' WJB ' cannot constitute a meaningful expression alone;
The correct understanding is: When $_get[' abc ' is empty (also whether, PHP ", Null,0,undifine, are equivalent Boolean false), the $STR is assigned to ' Wangjinbo ', otherwise assigned to ' WJB ';
Logical operators:
Such as:
| The code is as follows |
Copy Code |
$a =10; $b = 7; if ($a ++>8 | | $b ++>7) {//$a ++>8 is true, $b ++>7 This will not be executed Echo ' ok! '; } Echo ' a= '. $a. ' b= '. $b; Output ok,a=11,b=7 Change it. $a =10; $b = 7; if ($a ++>10 && $b ++>7) {//$a ++>8 is false, $b ++>7 This will not be executed Echo ' ok! '; } Echo ' a= '. $a. ' b= '. $b; A=11,b=7 |
Details: And && both represent logic and, where are their differences?
Mainly in the priority level above
Priority of and
| The code is as follows |
Copy Code |
and< = <&& or < = < | | Such as: $a =false | | True && > = > and; compare false First | | True, then assign the value $b =false or true; //|| > = > or; Assign a value $b=false, then compare, so the result is false Var_dump ($a, $b); BOOL (TRUE) bool (false) |
Bitwise operators
Displacement is a mathematical operation in PHP. Bits that are moved in any direction are discarded. The right side of the left shift is filled with 0, and the sign bit is removed, which means the signs are not retained. When you move right, the left side is filled with a symbol bit, which means the sign is retained.
Use parentheses to ensure that you want the priority. For example $a & $b = = True to first compare and then bitwise AND ($a & $b) = = True to first bitwise and then compare.
Be aware of conversions of data types. If both the left and right arguments are strings, the bitwise operator operates on the ASCII value of the character.
The INI setting for PHP error_reporting uses the bitwise value,
Provides a real-world example of closing a bit. To display in addition to the prompt level
All errors outside of php.ini are used in this way:
E_all & ~e_notice
The specific mode of operation is to obtain the value of E_all first:
00000000000000000111011111111111
Then get the value of E_notice:
00000000000000000000000000001000
Then, through the ~ to reverse it:
11111111111111111111111111110111
Finally, a bit with a bitwise AND and (&) of two values is set (1):
00000000000000000111011111110111
Another method is to use bitwise XOR (^) to obtain only the
The bits set in one of the values:
E_all ^ E_notice
Error_reporting can also be used to demonstrate how to set the position. Show only errors and recoverable
The wrong method is:
E_error | E_recoverable_error
Which means E_error
00000000000000000000000000000001
and E_recoverable_error
00000000000000000001000000000000
Use the bitwise OR OR (|) operator to get the result that is set in any one of the values:
00000000000000000001000000000001
Example the And,or and XOR bitwise operators #1 integers
| The code is as follows |
Copy Code |
/* * 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 <<<> --------- --------- -- --------- Result value op Test --------- --------- -- --------- EOH; /* * Here is 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 Inclusive 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 routines 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 = +) & (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 and a string or compare strings that involve numeric content, the strings are converted to numeric values and compared to numeric values. This rule also applies to switch statements. The type conversion is not performed when compared with = = = or!==, because the type and the value are compared at this time.
| The code is as follows |
Copy Code |
Var_dump (0 = = "a"); 0 = = 0-True Var_dump ("1" = = "01"); 1 = = 1-True Var_dump ("ten" = = "1e1"); Ten = = True Var_dump (+ = "1e2"); + = True Switch ("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 (in order) according to the following table.
Compare multiple types
| Number of operations 1 types |
Number of Operations 2 types |
Results |
| null or string |
String |
NULL Convert to "" for numeric or lexical comparisons |
| bool or null |
Any other type |
Convert to bool, FALSE <TRUE |
| Object |
Object |
Built-in classes can define their own comparisons, not homogeneous cannot% |
http://www.bkjia.com/PHPjc/628810.html www.bkjia.com true http://www.bkjia.com/PHPjc/628810.html techarticle arithmetic operator 1, arithmetic operator: + 、-、 *,/,%. 2, Increment/decrement operators: such as $a++, $a--, + + $a,--$a. Such as: Code to copy Code as follows? PHP $a = 10; $b = 5; $c = $a + +; First Fu ...