Home >Backend Development >PHP Tutorial >Operators - PHP Manual Notes
Operator precedence
Every programming language has operators, and operators must be learned to use flexibly.
Operators have different priorities and combination directions.
<code><?php var_dump(1 <= 1 == 1); // true var_dump(true ? 0 : true ? 1 : 2); // 2 $a = 1; var_dump(++$a + $a++); // may print 4 or 5</code>
Use parentheses when necessary to enhance the readability of your code.
Arithmetic operators
The result of the modulo operator has the same sign as the dividend.
Theassignment operator copies the value of the original variable to the new variable. The exception is that when an object is encountered, the value is assigned by reference unless the clone
keyword is explicitly used to copy.
The new operator automatically returns a reference.
bit operators
Displacement has the following rules:
The focus of this section is to understand the several example programs that are the focus of the manual. The XOR operation of strings in the example is difficult to understand. We will look at this later. There are also integer displacements, I feel like I understand them well.
Comparison operators
Ordinary equal sign==
As long as the two values are equal after type conversion, it will return true.
If comparing a number and a string or comparing strings involving numeric content, the string will be converted to a numeric value and the comparison will be performed as a numeric value.
<code><?php var_dump(0 == "a"); // true var_dump("1" == "01"); // true var_dump("10" == "1e1"); // true</code>
Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3
returns expr1 if expr1 is true, otherwise it returns expr3. Ternary operators are evaluated from left to right.
Error Control Operator
PHP supports an error control operator @, which is only valid for expressions. By placing it before an expression, any error messages the expression may produce are ignored.
It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach.
Execution operator
PHP supports an execution operator: the backtick, which is the one in the upper left corner of the keyboard. The effect is the same as the function shell_exec()
.
<code><?php $output = `systeminfo`; $outip = shell_exec('ipconfig'); echo "<pre class="brush:php;toolbar:false">$outip"; echo "
$output"; The
backtick operator is invalid when safe mode is activated or shell_exec()
is turned off.
Attention! Backticks cannot be used in double-quoted strings.
increment decrement operator
Increment/decrement operators do not affect boolean values.
Decreasing the NULL value has no effect, but the result of incrementing NULL is 1.
When dealing with arithmetic operations on character variables, PHP follows Perl's habits instead of C's. For example, in Perl $a = 'Z'; $a++;
will turn $a into 'AA'.
Attention! Character variables can only be incremented, not decremented, and only support pure letters (a-z and A-Z). Incrementing/decrementing other character character variables will be invalid, and the original string will not change.
<code><?php $z = 'z'; $Z = 'Z'; var_dump(++$z); // 'aa' var_dump(++$Z); // 'AA'</code>
Logical operators
||
has higher priority than or
. &&
has higher priority than and
.
String operators
The first one is the connection operator .
, and the second one is the connection assignment operator .=
.
Array operators
Union: $a + $b
. Append the right array elements to the left array. If the keys in both arrays are present, only the keys in the left array will be used, and any changes will be ignored.
Equal: $a == $b
. have the same key-value pairs.
Congruent: $a === $b
. Have the same key-value pairs, in the same order and type.
does not vary: $a != $b
or $a <> $b
.
Not congruent: $a !== $b
.
If the cells in the array have the same key name and value, they will be equal when compared. Don't care about the order and type.
<code><?php $a = array("apple", "banana"); $b = array(1 => "banana", "0" => "apple"); var_dump($a); var_dump($b); var_dump($a == $b); var_dump($a === $b);</code>
The output results are as follows.
<code>array (size=2) 0 => string 'apple' (length=5) 1 => string 'banana' (length=6) array (size=2) 1 => string 'banana' (length=6) 0 => string 'apple' (length=5) boolean true boolean false</code>
Type operators
There is a type operator instanceof
in PHP, which is used to determine whether a PHP variable belongs to an instance of a certain class.
<code><?php class MyParent {} class MyClass extends MyParent {} class NotMyClass {} interface MyInterface {} class InClass implements MyInterface {} $a = new MyClass; var_dump($a instanceof MyClass); // true var_dump($a instanceof NotMyClass); // false var_dump($a instanceof MyParent); // true $b = new InClass; var_dump($b instanceof MyInterface); // true $c = 'InClass'; var_dump($b instanceof $c); // true var_dump($c instanceof stdClass); // false</code>
Note that instanceof
is not allowed to be used to detect constants.
(Full text ends)
The above introduces the operators - PHP manual notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.