The operators we use in PHP include arithmetic operators, assignment operators, comparison operators, logical operators, and so on. Let me introduce you to your friends.
Arithmetic operators
The division operator always returns a floating-point number. The exception is the following: two operands are integers (or integers converted into strings) and exactly divisible, when it returns an integer.
The operands of the modulo operator are converted to integers (minus fractional parts) before the operation.
Note: The result of modulo $a% $b is negative when the $a is positive.
Cases:
The code is as follows |
Copy Code |
/* Tested under PHP 5.2.6-1 with Suhosin-patch 0.9.6.2 (CLI) on both i386 and AMD64, Debian Lenny/sid */ $a = 2863311530; $b = 256; $c = $a% $b; echo "$c n "; Echo (2863311530 256). " n "; /* Directly with no variables, just to be sure */ ?> |
operator |
Description |
Example |
Results |
+ |
Addition |
x=2 X+2 |
4 |
- |
Subtraction |
x=2 5-x |
3 |
* |
Multiplication |
X=4 X*5 |
20 |
/ |
Division |
15/5 5/2 |
3 2.5 |
% |
Modulus (Division remainder) |
5%2 10%8 10%2 |
1 2 0 |
++ |
Increment |
X=5 X + + |
X=6 |
-- |
Decrement |
X=5 x-- |
X=4 |
Assignment operators
The basic assignment operator is "=". At first you might think it's "equal", not really. It actually means assigning the value of the right expression to the left operand.
The value of the assignment expression is the value assigned. In other words, the value of "$a = 3" is 3. This allows you to do some tips:
The code is as follows |
Copy Code |
$a = ($b = 4) + 5; $a is now 9, and $b has become 4. ?> |
In addition to the basic assignment operator, there are "combined operators" that are appropriate for all two-tuple arithmetic, array collections, and string operators, so that you can use its value in an expression and assign the result of the expression to it, for example:
The code is as follows |
Copy Code |
$a = 3; $a + = 5; Sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello"; $b. = "there!"; Sets $b to "Hello there!", just like $b = $b. "There!"; ?> |
Note that the assignment operation copies the value of the original variable into a new variable (value assignment), so changing one does not affect the other. This is also suitable for copying some values such as large arrays in a very dense loop. You can also use reference assignment, with $var = & $othervar; Grammar. A reference assignment means that both variables point to the same data, without any copy of the data. For more information on references, see the description of the reference. In PHP 5, objects are always assigned by reference, unless you explicitly use the new clone keyword
operator |
Description |
Example |
= |
X=y |
X=y |
+= |
X+=y |
X=x+y |
-= |
X-=y |
X=x-y |
*= |
X*=y |
X=x*y |
/= |
X/=y |
x=x/y |
.= |
X.=y |
X=x.y |
%= |
X%=y |
X=x%y |
Comparison operators
Comparison operators, as implied by their names, allow two values to be compared. If you compare an integer and a string, the string is converted to an integer. If you compare two numeric strings, they are compared as integers. This rule also applies to switch statements.
The code is as follows |
Copy Code |
Var_dump (0 = = "a"); 0 = = 0-True Var_dump ("1" = = "01"); 1 = = 1-True Var_dump ("1" = = "1e0"); 1 = = 1-True Switch ("a") { Case 0: echo "0"; Break Case ' a ':///Never reached because "a" is already matched with 0 echo "a"; Break } ?> |
operator |
Description |
Example |
== |
is equal to |
5==8 returns False |
!= |
is not equal |
5!=8 returns True |
> |
is greater than |
5>8 returns False |
< |
is less than |
5<8 returns True |
>= |
is greater than or equal to |
5>=8 returns False |
<= |
is less than or equal to |
5<=8 returns True |
logical operators
There are two different form operators for "and" and "or" because their operations have different precedence (see operator precedence).
Example #1 Logical Operator Example
The code is as follows |
Copy Code |
The following foo () is not called because they are "shorted" by the operator. $a = (false && foo ()); $b = (true | | foo ()); $c = (False and foo ()); $d = (true or foo ()); "| |" has a higher priority than "or" $e = False | | True $e is assigned a value of (false | | true), and the result is true $f = False or true; $f is assigned false [Altair NOTE: ' = ' has a higher precedence than ' or '] Var_dump ($e, $f); "&&" has a higher priority than "and" $g = True && false; $g is assigned (true && false) and the result is false $h = True and false; $h is assigned true [Altair Note: ' = ' has a higher precedence than ' and '] Var_dump ($g, $h); ?> The output of the above routines is similar to the following: BOOL (TRUE) BOOL (FALSE) BOOL (FALSE) BOOL (TRUE) |
operator |
Description |
Example |
&& |
and |
X=6 Y=3(x < && y > 1) returns True |
|| |
Or |
X=6 Y=3(x==5 | | y==5) returns false |
! |
Not |
X=6 Y=3! (x==y) returns True |
http://www.bkjia.com/PHPjc/628919.html www.bkjia.com true http://www.bkjia.com/PHPjc/628919.html techarticle The operators we use in PHP include arithmetic operators, assignment operators, comparison operators, logical operators, and so on. Let me introduce you to your friends. Arithmetic operator division ...