PHP Operators
Operators are symbols that indicate a specific operation to be performed on one or more operands. PHP has a wide range of operators, including arithmetic operators, assignment operators, comparison operators, logical operators, and conditional operators. In this article, we will cover each type of operator in detail.
Arithmetic Operators
Arithmetic operators in PHP are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. The following table shows all the arithmetic operators in PHP:
Operator | Name | Example |
---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulus | $a % $b |
++ | Increment | ++$a |
-- | Decrement | --$a |
Example
<?php
$a = 10;
$b = 5;
// Addition
$c = $a + $b;
echo "Addition of $a and $b is: $c" . "<br>";
// Subtraction
$c = $a - $b;
echo "Subtraction of $a and $b is: $c" . "<br>";
// Multiplication
$c = $a * $b;
echo "Multiplication of $a and $b is: $c" . "<br>";
// Division
$c = $a / $b;
echo "Division of $a and $b is: $c" . "<br>";
// Modulus
$c = $a % $b;
echo "Modulus of $a and $b is: $c" . "<br>";
// Increment
echo "Increment of $a is: " . ++$a . "<br>";
// Decrement
echo "Decrement of $b is: " . --$b . "<br>";
?>
Output
Addition of 10 and 5 is: 15
Subtraction of 10 and 5 is: 5
Multiplication of 10 and 5 is: 50
Division of 10 and 5 is: 2
Modulus of 10 and 5 is: 0
Increment of 10 is: 11
Decrement of 5 is: 4
Assignment Operators
Assignment operators in PHP are used to assign values to variables. The following table shows all the assignment operators in PHP:
Operator | Example | Same As |
---|---|---|
= | $a = $b | $a = $b |
+= | $a += $b | $a = $a + $b |
-= | $a -= $b | $a = $a - $b |
*= | $a *= $b | $a = $a * $b |
/= | $a /= $b | $a = $a / $b |
%= | $a %= $b | $a = $a % $b |
Example
<?php
$a = 10;
$b = 5;
// Simple assignment
$c = $a;
echo "Value of c: $c<br>";
// Add and assignment
$a += $b;
echo "Value of a (after a += b): $a>";
// Subtract and assignment
$a -= $b;
echo "Value of a (after a -= b): $a<br>";
// Multiply and assignment
$a *= $b;
echo "Value of a (after a *= b): $a<br>";
// Divide and assignment
$a /= $b;
echo "Value of a (after a /= b): $a<br>";
// Modulus and assignment
$a %= $b;
echo "Value of a (after a %= b): $a<br>";
?>
Output
Value of c: 10
Value of a (after a += b): 15
Value of a (after a -= b): 10
Value of a (after a *= b): 50
Value of a (after a /= b): 10
Value of a (after a %= b): 0
Comparison
Comparison operators in PHP are used to compare two values. The result of a comparison is a boolean value, either true or false. The following table shows all the comparison operators in PHP:
Operator | Name | Example |
---|---|---|
== | Equal | $a == $b |
=== | Identical | $a === $b |
!= | Not Equal | $a != $b |
<> | Not Equal | $a <> $b |
!== | Not Identical | $a !== $b |
< | Less Than | $a < $b |
> | Greater Than | $a > $b |
<= | Less Than or Equal To | $a <= $b |
>= | Greater Than or Equal To | $a >= $b |
Example
<?php
$a = 10;
$b = 5;
// Equal
if($a == $b) {
echo "$a is equal to $b<br>";
} else {
echo "$a is not equal to $b<br>";
}
// Identical
if($a === $b) {
echo "$a is identical to $b<br>";
} else {
echo "$a is not identical to $b<br>";
}
// Not Equal
if($a != $b) {
echo "$a is not equal to $b<br>";
} else {
echo "$a is equal to $b<br>";
}
// Not Equal With <>
if($a <> $b) {
echo "$a is not equal to $b<br>";
} else {
echo "$a is equal to $b<br>";
}
// Not Identical
if($a !== $b) {
echo "$a is not identical to $b<br>";
} else {
echo "$a is identical to $b<br>";
}
// Less than
if($a < $b) {
echo "$a is less than $b<br>";
} else {
echo "$a is greater than or equal to $b<br>";
}
// Greater than
if($a > $b) {
echo "$a is greater than $b<br>";
} else {
echo "$a is less than or equal to $b<br>";
}
// Less than or equal to
if($a <= $b) {
echo "$a is less than or equal to $b<br>";
} else {
echo "$a is greater than $b<br>";
}
// Greater than or equal to
if($a >= $b) {
echo "$a is greater than or equal to $b<br>";
} else {
echo "$a is less than $b<br>";
}
?>
Output
10 is not equal to 5
10 is not identical to 5
10 is not equal to 5
10 is not equal to 5
10 is not identical to 5
10 is greater than or equal to 5
10 is greater than 5
10 is greater than or equal to 5
10 is greater than or equal to 5
Logical Operators
Logical operators in PHP are used to combine multiple expressions and produce a boolean result. The following table shows all the logical operators in PHP:
Operator | Name | Example |
---|---|---|
and | And | $a and $b |
or | Or | $a or $b |
xor | Xor | $a xor $b |
! | Not | !$a |
&& | And | $a && $b |
|| | Or | $a || $b |
Example
<?php
$a = true;
$b = false;
// And
if($a and $b) {
echo "Both a and b are true";
} else {
echo "Either a or b is false";
}
// Or
if($a or $b) {
echo "Either a or b is true";
} else {
echo "Both a and b are false";
}
// Xor
if($a xor $b) {
echo "Either a or b is true, but not both";
} else {
echo "Both a and b are either true or false";
}
// Not
if(!$a) {
echo "a is false";
} else {
echo "a is true";
}
// And with &&
if($a && $b) {
echo "Both a and b are true";
} else {
echo "Either a or b is false";
}
// Or with ||
if($a || $b) {
echo "Either a or b is true";
} else {
echo "Both a and b are false";
}
?>
Output
Either a or b is false
Either a or b is true
Either a or b is true, but not both
a is true
Either a or b is false
Either a or b is true
Conditional Operator
The conditional operator in PHP is also known as the ternary operator. It is used to assign one value to a variable if a condition is true, and another value if the condition is false. The syntax for the conditional operator is as follows:
(condition)?value1:value2
Example
<?php
$a = 10;
$b = 5;
$max = ($a > $b)? $a : $b;
echo "The maximum value between $a and $b is: $max";
?>
Output
The maximum value between 10 and 5 is: 10
Important Points
- PHP has a wide range of operators, including arithmetic operators, assignment operators, comparison operators, logical operators, and conditional operators.
- Arithmetic operators are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.
- Assignment operators are used to assign values to variables.
- Comparison operators are used to compare two values.
- Logical operators are used to combine multiple expressions and produce a boolean result.
- The conditional operator is used to assign one value to a variable if a condition is true, and another value if the condition is false.
Summary
Operators are an essential part of any programming language, and PHP is no exception. PHP has a wide range of operators, including arithmetic, assignment, comparison, logical, and conditional operators. Understanding these operators and how they work is crucial for writing effective PHP code.