1. Arithmetic operators
- + (Add) $a + $b
- - (Subtract) $a - $b
- * (Multiple) $a * $b
- /(Divide) $a / $b
- % (Remainder) $ a % $b
-
Copy code
2. String operators
- . (dot) (the only string operator in php)
-
Copy code
3. Assignment operator
1. Simple assignment operator
2. Compound assignment operator
- += $a += $b is equivalent to $a = $a + $b
- -= $a -= $b is equivalent to $a = $a - $b
- *= $a *= $b is equivalent to $a = $a * $b
- /+ $a /= $b is equivalent to $a = $a / $b
- %= $a %= $b is equivalent to $a = $a % $b
- .= $a .= $b is equivalent to $a = $a . $b
-
Copy code
3. Pre-increment and decrement operations and post-increment and decrement operations
- $a++ The value of $a itself has not changed, but the value of the entire expression will become $a + 1
- ++$a The value of $a itself has changed, $a is first replaced by $a = $a + 1, and then return $a + 1
- $a-- The value of $a itself has not changed, but the value of the entire expression will become $a - 1
- --$a The value of $a itself has changed, $a First $a = $a - 1, then return $a + 1
-
Copy code
4. Reference operator
The reference operator & can be used in associative assignment. Usually, when assigning the value of one variable to another variable, a copy of the metavariable is first made and then stored elsewhere in memory. For example:
In the above example, the first line of code assigns a value to $a; the second line of code first generates a copy of $a and then saves it in $b. If the value of $a is subsequently changed, the value of $b will not change. Looking at the example below:
- $a = 5;
- $b = &$a;
- $a = 7; // $a and $b are now both 7
-
Copy code
Note: The reference is not an independent second pointer, but a pointer using the original variable, that is, both $a and $b point to the same address in the memory. In the above example, the second line is $a referenced by $b. When the value of $a in the third line changes, the $b that referenced it also changes. We can break this reference association by resetting:
Note: This reset only resets $a, it does not change the value of $b(7). unsert($a) only destroys the association between $a and the value 7 stored in memory. Unsert($a) can be understood as canceling $a.
4. Comparison operators
Comparison operators return logical Boolean values: true or false.
- ==(equal to)
- ===(constantly equal to)
- !=(not equal to)
- !==(not equal to)
- <>(not equal to)
- <(less than)
- > ;(greater than)
- <==(less than or equal to)
- >==(greater than or equal to)
-
Copy code
5. Logical operators
- ! (Not)
- && (AND)
- || (OR)
- and (AND)
- or (OR)
- xor (XOR) $a xor $b If $a or $b is true, then Return true. If $a and $b are both true or both false, return false.
-
Copy code
Note: and and or have lower priority than && and ||.
6. Bit operators
Bit operators can treat an integer variable as a sequence of bits (Bits). Bitwise operators are not used often.
- & (bitwise AND) $a & $b The result of ANDing each bit of $a and $b
- | (bitwise OR) $a | $b The result of ANDing each bit of $a and $b The result obtained by performing the "OR" operation on each bit of b
- ~ (bitwise NOT) ~$a The result obtained by performing the "NOT" operation on each bit of $a
- ^ (bitwise XOR) $a ^ $ b The result of performing the "XOR" operation on each bit of $a and $b
- << (left shift) $a << $b Shift $a to the left by $b bits
- >> (right shift) $a >> $b Shift $a right by $b
-
Copy code
7. Other operators
- , (comma) is used to separate function parameters or other list items. This operator is often used incidentally (not independently).
- new (initializing an instance of a class)
- -> (accessing members of a class)
-
Copy code
1. Ternary operator?:
- condition ? value if true : value if false
-
Copy code
The ternary operator can be seen as the abbreviation of if else conditional statement.
2. Error suppression operator
The error suppression operator @ can be used before any expression, that is, before any expression that has a value or can be calculated, for example:
If the error suppression operator @ is not used in the above example, then this line of code will throw a divide-by-0 warning. If @ is used, the warning will be suppressed, that is, not thrown.
If some warnings are suppressed through this method and a warning is encountered, it needs to be handled through the error handling statements we have written in advance.
If the track_errors feature in php.ini is enabled, error messages will be stored in the global variable $php_errormsg.
3. Execution operator
- `` (a pair of back single quotes) The execution operator is actually a pair of operators, a pair of back single quotes.
-
Copy code
php will try to execute commands between back single quotes as server-side commands. The value of the expression is the result of the command execution. For example, in a unix system, you can use:
- $out = `ls -la`;
- echo '
' . $out . ' ';
-
Copy code
On Windows server, you can use:
- $out = `dir c:`;
- echo '
' . $out . ' ';
-
Copy code
In both cases, a directory list will be obtained and the list will be saved in $out. Then, the list will be displayed in the browser or processed by other methods.
4. Array operators
Note: In the syntax description below, $a and $b are not ordinary scalar values, but array types
- + (Union) $a + $b Returns an array containing all elements in $a and $b
- == (Equivalent) $ == $b if $a and $b have the same key value Yes, return true
- === (identity) $a === $b If $a and $b have the same key-value pair and the same order, return true
- != (non-equivalence) $a != $b If $a and $b are not equivalent, return true
- <> (not equivalent) $a <> $b If $a and $b are not equivalent, return true
- !== (Non-Identity) $ !== $b If $a and $b are not identical, return true
-
Copy code
5. Type operator
instanceof (the only type operator), this operator is used in object-oriented programming.
The instanceof operator allows checking whether an object is an instance of a specific class. For example:
- class sampleClass();
- $myObject = new sampleClass();
- if ($myObject instanceof sampleClass) {
- echo 'myObject is an instance of sampleClass';
- }
- ?>
-
Copy code
|