PHP Operators
In PHP, the assignment operator = is used to assign a value to a variable.
In PHP, the arithmetic operator + is used to add values together.
PHP arithmetic operators
operator |
name |
Description |
Example |
Results |
X + y |
Add |
X and Y's and |
2 + 2 |
4 |
X-y |
Reducing |
The difference between x and Y |
5-2 |
3 |
X * y |
By |
The product of X and Y |
5 * 2 |
10 |
X/Y |
Except |
Quotient of X and Y |
15/5 |
3 |
X% y |
Modulo (remainder of division) |
The remainder of x divided by Y |
5% 2 10% 8 10% 2 |
1 2 0 |
-X |
Take counter |
X inverse |
-2 |
|
A. B |
collocated |
Connect two strings |
"Hi". "Ha" |
Hiha |
The following example shows the different results obtained using different arithmetic operators:
Instance
1<?PHP2 $x=10; 3 $y=6;4 Echo($x+$y);//Output5 Echo($x-$y);//Output 46 Echo($x*$y);//Output7 Echo($x/$y);//Output 1.66666666666678 Echo($x%$y);//Output 49?>
PHP Assignment operators
In PHP, the basic assignment operator is "=". It means that the left operand is set to the value of the right-hand expression. In other words, the value of "$x = 5" is 5.
operator |
equivalent to |
Description |
x = y |
x = y |
The left operand is set to the value of the right-hand expression |
x + = y |
x = x + y |
Add |
X-= y |
x = XY |
Reducing |
X *= y |
x = x * y |
By |
X/= y |
× = x/y |
Except |
X%= y |
x = x% y |
Modulo (remainder of division) |
A. = b |
A = A. B |
Connect two strings |
The following example shows the different results from using different assignment operators:
Instance
1<?PHP2 $x=10; 3 Echo $x;//Output Ten4 5 $y=20; 6 $y+ = 100;7 Echo $y;//Output8 9 $z=50;Ten $z-= 25; One Echo $z;//Output A - $i=5; - $i*= 6; the Echo $i;//Output - - $j=10; - $j/= 5; + Echo $j;//Output 2 - + $k=15; A $k%= 4; at Echo $k;//Output 3 -?>
The following example shows the different results from using different string operators:
Instance
1<?PHP2 $a= "Hello";3 $b=$a. "World!";4 Echo $b;//Output Hello world!5 6 $x= "Hello";7 $x. = "world!";8 Echo $x;//Output Hello world!9?>
PHP Increment/decrement operator
operator |
name |
Description |
+ + X |
Pre-increment |
X plus 1, then return x |
X + + |
Post-increment |
return x, then x plus 1 |
--X |
Pre-decrement |
X minus 1, then return x |
X-- |
After descending |
return x, then x minus 1 |
The following example shows the results obtained using the increment/decrement operator:
Instance
1<?PHP2 $x=10; 3 Echo++$x;//output one by one4 5 $y=10; 6 Echo $y++;//Output Ten7 8 $z=5;9 Echo--$z;//Output 4Ten One $i=5; A Echo $i--;//Output 5 -?>
PHP comparison Operators
Comparison operators allow you to compare two values:
operator |
name |
Description |
Example |
x = = y |
Equals |
Returns True if X equals y |
5==8 return False |
x = = y |
Constant equals |
Returns True if X equals Y and they are of the same type |
5=== "5" returns false |
X! = y |
Not equal to |
Returns True if X is not equal to Y |
5!=8 returns True |
X <> y |
Not equal to |
Returns True if X is not equal to Y |
5<>8 returns True |
X!== y |
Not constant equals |
Returns True if X is not equal to Y, or if they are of different types |
5!== "5" returns True |
X > Y |
Greater than |
Returns True if X is greater than Y |
5>8 return False |
X < y |
Less than |
Returns True if X is less than Y |
5<8 returns True |
X >= y |
Greater than or equal |
Returns True if X is greater than or equal to Y |
5>=8 return False |
X <= y |
Less than or equal |
Returns True if X is less than or equal to Y |
5<=8 returns True |
The following example shows the different results from using some comparison operators:
Instance
1<?PHP2 $x=100; 3 $y= "100";4 5 Var_dump($x==$y);6 Echo"<br>";7 Var_dump($x===$y);8 Echo"<br>";9 Var_dump($x!=$y);Ten Echo"<br>"; One Var_dump($x!==$y); A Echo"<br>"; - - $a=50; the $b=90; - - Var_dump($a>$b); - Echo"<br>"; + Var_dump($a<$b); -?>
PHP logical operators
operator |
name |
Description |
Example |
X and Y |
And |
Returns true if both X and Y are true |
X=6 Y=3 (x < ten and y > 1) returns True |
X or Y |
Or |
Returns True if X and Y have at least one true |
X=6 Y=3 (x==6 or y==5) returns True |
X XOR y |
XOR or |
Returns True if X and Y have and only one is true |
X=6 Y=3 (X==6 xor y==3) returns false |
X && y |
And |
Returns true if both X and Y are true |
X=6 Y=3 (x < ten && y > 1) returns True |
x | | Y |
Or |
Returns True if X and Y have at least one true |
X=6 Y=3 (x==5 | | y==5) returns false |
! X |
Non - |
Returns True if X is not true |
X=6 Y=3 ! (x==y) returns True |
PHP Array Operators
operator |
name |
Description |
X + y |
Collection |
Collections of X and Y |
x = = y |
Equal |
Returns True if X and Y have the same key/value pair |
x = = y |
Identical |
Returns True if X and Y have the same key/value pair and have the same type in the same order |
X! = y |
Not equal |
Returns True if X is not equal to Y |
X <> y |
Not equal |
Returns True if X is not equal to Y |
X!== y |
Bu Heng, etc. |
Returns True if X is not equal to Y |
The following example shows the different results from using some array operators:
Instance
1<?PHP2 $x=Array("A" and "Red", "b" = "green")); 3 $y=Array("c" = "Blue", "d" = "Yellow"); 4 $z=$x+$y;//merging $x and $y arrays5 Var_dump($z);6 Var_dump($x==$y);7 Var_dump($x===$y);8 Var_dump($x!=$y);9 Var_dump($x<>$y);Ten Var_dump($x!==$y); One?
9. PHP Tutorial _php operator