PHP / PHP Operators
They are used to perform operations on variables and values.
Type of operators in PHP
| S. No |
Type of operator |
| 1 |
Arithmetic operators |
| 2 |
Assignment operators |
| 3 |
Comparison operators |
| 4 |
Increment/Decrement operators |
| 5 |
Logical operators |
| 6 |
String operators |
| 7 |
Conditional assignment operators |
Arithmetic operators
| Program |
Output |
Output |
| Operator |
Operation |
Example |
| + |
Addition |
$x + $y |
| - |
Subtraction |
$x - $y |
| * |
Multiplication |
$x * $y |
| / |
Division |
$x / $y |
| % |
Modulus |
$x % $y |
1. Write a php program to use all php Arithmetic operators.
| Program |
Output |
|
|
Entered Numbers 10
5
Addition 15
Substration 5
Mulitiply 50
Division 2
Modulus 0
|
Assignment operators
| Operator |
Operator Details |
Equivalent |
| x = y |
Y value is assigned to X. |
x = y |
| x += y |
Addition |
x = x+ y |
| x -= y |
Subtraction |
x = x - y |
| x *= y |
Multiplication |
x = x * y |
| x /= y |
Division |
x = x / y |
| x %= y |
Modulus |
x = x % y |
1. Write a php program to use all php Assignment operators.
| Program |
Output |
|
|
Entered Numbers 10
20
30
Assignment Operator 1010
Division 0.5
Modulus 0
Multiplication 200
Addition 20
Subtraction 0
|
Comparison operators
| Operation |
Operation Details |
Example |
| == |
Equal |
if $x is equal to $y, it returns true otherwise false |
| != |
Not equal |
if $x is not equal to $y, it returns true otherwise false |
| > |
Greater than |
if $x is greater than $y, it returns true otherwise false |
| < |
Less than |
if $x is less than $y, it returns true otherwise false |
| >= |
Greater than or equal to |
if $x is greater than or equal to $y, it returns true otherwise false |
| <= |
Less than or equal to |
if $x is less than or equal to $y, it returns true otherwise false |
1. Write a php program to use all php Comparison operators.
| Program |
Output |
|
|
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
|
Increment/Decrement operators
| Operator |
Operator Details |
| ++$x |
Pre-increment |
| $x++ |
Post-increment |
| --$x |
Pre-decrement |
| $x-- |
Post-decrement |
| Program |
Output |
|
|
Entered Number 20
19
20
20
19
|
Logical Operators
| Operation Name |
Operator Name |
Details |
| and |
And |
True if both $x and $y are true |
| or |
Or |
True if either $x or $y is true |
| xor |
Xor |
True if either $x or $y is true, but not both |
| && |
And |
True if both $x and $y are true |
| || |
Or |
True if either $x or $y is true |
| ! |
Not |
True if $x is not true |
1. Write a php program to use all php Comparison operators.
| Program |
Output |
|
|
And Operator
Or Operator
xor Operator
And Operator
Or Operator
Not Operator
|
String operators
| Operator |
Name |
| . |
Concatenation |
| .= |
Concatenation assignment |
1. Write a php program to use all php String operators.
| Program |
Output |
|
|
PHP LANGUAGE!
|
|
|
PHP LANGUAGE!
|
PHP Conditional Operator
?: is called as Ternary operator.
1. Write a php program to use Ternary Operator to find big or small of 2 numbers.
|