The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.
3.4.1 Arithmetic Operators
1 <?php 2 $num1 = -10; 3 $num2 = -4; 4 $num3 = $num1%$num2; 5 $num4 = $num1/$num2; 6 var_dump($num3); //int -2 7 echo "<br/>"; 8 var_dump($num4); //float 2.5 9 ?>
Output:
. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 6: int -2 2 . 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 8: a float 2.5
3.4.2 increment / decrement operators
++ $ a: the value of $ a plus one, then returns $ a
$ A ++: return $ a, then the value of $ a plus 1
1 ? < PHP 2 $ num1 = 2 ; 3 $ num2 = ++ $ num1 ; // $ num1 first since plus 1, then assigns the result to num2 $ 4 $ num3 = 2 ; 5 $ Num4 = $ num3 ++ ; // first value is assigned num3 $ $ num4, and then adding from $ num3. 1 . 6 . 7 echo '$ num1 =', $ num1 ; // . 3 . 8 echo "a" ; . 9 echo '$ num2 = ', $ num2 ; // . 3 10 echo "a" ; 11 echo '$num3 =',$num3; //3 12 echo "<br/>"; 13 echo '$num4 =',$num4; //2 14 echo "<br/>"; 15 ?>
Output:
1 $ num1 = 3 2 $ num2 = 3 3 $ num3 = 3 4 $ num4 = 2
3.4.3 Assignment Operators
PHP combinations of operators: + =, - =, * =, / =,% =, = like.
program:
1 <?php 2 var_dump($a = ($b = 4) + 5); //int 9 3 echo "<br/>"; 4 echo $a; 5 echo "<br/>"; 6 echo $b; 7 ?>
Output:
. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 2: int. 9 2 . 3 . 9 . 4 . 4
program:
1 <?php 2 $a = 5; 3 $a += 3; 4 echo $a; 5 echo "<br/>"; 6 $a *= 2; 7 echo $a; 8 echo "<br/>"; 9 $a /=2; 10 echo $a; 11 echo "<br/>"; 12 ?>
Output:
1 8 2 16 3 8
3.4.4 Comparison operators
=== congruent
Ranging from <>
Non-congruent! ==
program:
1 <?php 2 $a = 5; 3 $b = "5.0"; 4 var_dump($a==$b); //boolean true 5 echo "</br>"; 6 var_dump($a===$b); //boolean false 7 echo "</br>"; 8 var_dump($a!=$b); //boolean false 9 echo "</br>"; 10 var_dump($a!==$b); //boolean true 11 echo "</br>"; 12 var_dump($a>=$b); //boolean true 13 echo "</br>"; 14 var_dump($a<=$b); //boolean true 15 echo "</br>"; 16 17 var_dump($a<>$b); //boolean false 18 echo "</br>"; 19 ?>
Output:
. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 4: Boolean to true 2 . 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 6: Boolean to false . 4 . 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 8: Boolean to false . 6 . 7 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: Boolean to true . 8 . 9 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12 is: Boolean to true 10 . 11 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 14: Boolean to true 12 is 13D: \ wampServer \ www \ Apache server home directory \ practise \ routine .php: 17: boolean false
3.4.5 Logical Operators
Logic and | && | and |
Logical or | || | or |
Logical NOT | ! | |
Logical XOR | xor |
XOR logical xor: only two values a value is true, the result is true, otherwise false.
program:
1 <?php 2 $a = 3>2; 3 $b = 3>4; 4 $c = 3>1; 5 var_dump($a && $b); //boolean false 6 echo "<br/>"; 7 var_dump($a||$b); //boolean true 8 echo "<br/>"; 9 var_dump(!$a); //boolean false 10 echo "<br/>"; 11 var_dump($a xor $b); //boolean true 12 echo "<br/>"; 13 var_dump($a xor $c); //boolean false 14 ?>
Output:
. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 5: Boolean to false 2 . 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 7: Boolean to true . 4 . 5 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 9: Boolean to false . 6 . 7 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 11: Boolean to true . 8 9 D: \ WampServer \ the WWW \ the Apache server home directory \ practise \ routine .php: 13: boolean false
3.4.6 string concatenation operator
String concatenation operator is only one electrical operator ".", The "." Operator to connect a string into two strings.
program:
. 1 <? PHP 2 echo "Hello World!" "<Br />.". DATE ( "Y when m in MMM d h i s seconds min" ); 3 ?>
Output:
1 the Hello world! 2 2019 Nian 11 Yue 05 Ri 03 57 minutes and 15 seconds
3.4.7 operator error suppression
The "@" operator placed before the PHP expression of the expression produce any error messages will not be output. This has two advantages.
1. Safety: Avoid the error message exposed, resulting in vulnerabilities.
2. Appearance: Avoid browser page error message, affect the page appearance.
program:
. 1 ? < PHP 2 print $ Age ; // display variable information notice undefined . 3 echo "<br />" ; . 4 @ print $ Age ; @ @ shield undefined variables // notice information into print can not be replaced echo, otherwise there will be a program parse error 5 ?>
result:
Appears database connection, open a file stream, in addition to 0 abnormal, the @ symbol can be used to inhibit the expression or function of the error message.
3.4.8 conditional operator
The syntax is: Expression 1? Expression 2: Expression 3
Conditional operator is ternary operator
program:
1 <?php 2 $a = 0.0; 3 $b = ($a==0?"zero":"not zero"); 4 echo $b; 5 ?>
Output:
1 zero
3.4.9 Type Operators
instanceof for determining whether an object is a class object
program:
1 <?php 2 class A{ 3 } 4 class B{ 5 } 6 $a = new A(); 7 8 var_dump($a instanceof A); //boolean true 9 echo "<br/>"; 10 var_dump($a instanceof B); //boolean false 11 ?>
Output:
. 1 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 8: Boolean to true 2 . 3 D: \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: Boolean to false
3.4.10 Execution Operators
Operators performing Backticks (·) (typically below ESC key on the keyboard). Operator attempts to perform string contents backticks as operating system commands to perform (such as Linux or Windows shell commands DOS commands), and returns the results of the command system.
1 <?php 2 $cmd = `netstat -aon`; 3 print_r($cmd); 4 ?>
3.4.11 Bitwise Operators
Bit integer arithmetic operator is mainly used for data, when the expression contains bitwise operators, each operator will first integer conversion data to the corresponding binary number, and then bitwise operation.
image
program:
1 <?php 2 $a = 12; //12=00001100 3 $b = 3; // 3=00000011 4 echo $a & $b; //0 5 echo "<br/>"; 6 echo $a | $b; //15 7 echo "<br/>"; 8 echo $a ^ $b; //15 9 echo "<br/>"; 10 echo ~$a; //-13 11 echo "<br/>"; 12 echo $a << $b; //96 13 echo "<br/>"; 14 echo $a >> $b; //1 15 ?>
Output:
1 0 2 15 3 15 4 -13 5 96 6 1
3.4.12 operator precedence
image
Use brackets "()" is the most effective way to avoid the confusion of priorities.