PHP Operators
PHP Operators
Operators are symbols that enable you to use one or more values to produce a new value. A value that is operated on by an operator is referred to as an operand. An operator is a symbol or series of symbols that, when used in conjunction with values, performs an action and usually produces a new value. An operand is a value used in conjunction with an operator. There are usually two operands to one operator.
PHP language supports the following types of operators:
- Arithmetic operators
- Comparison operators
- Logical Operators
- Assignment Operators
- Conditional (or ternary) operators
- String operator
Arithmetic Operators
Operator |
Name |
Example |
Result |
+ | Addition | $a + $b | Sum of $a and $b |
- | Subtraction | $a - $b | Difference of $a and $b |
* | Multiplication | $a * $b | Product of $a and $b |
/ | Division | $a / $b | Quotient of $a divided by $b |
% | Modulus | $a % $b | Remainder of $a divided by $b |
** | Exponentiation | $a ** $b | $a raised to the power of $b |
<?php
$a = 10;
$b = 3;
// Examples
$sum = $a + $b;
echo "Addition: $a + $b = $sum\n";
$difference = $a - $b;
echo "Subtraction: $a - $b = $difference\n";
$product = $a * $b;
echo "Multiplication: $a * $b = $product\n";
$quotient = $a / $b;
echo "Division: $a / $b = $quotient\n";
$remainder = $a % $b;
echo "Modulus: $a % $b = $remainder\n";
$power = $a ** $b;
echo "Exponentiation: $a ** $b = $power\n";
?>
Comparison Operators
Operator |
Name |
Example |
Result |
== | Equal | $a == $b | true if $a is equal to $b |
=== | Identical | $a === $b | true if $a is equal to $b and of the same type |
!= | Not equal | $a != $b | true if $a is not equal to $b |
<> | Not equal (alternative) | $a <> $b | true if $a is not equal to $b |
!== | Not identical | $a !== $b | true if $a is not equal to $b or not of the same type |
< | Less than | $a < $b | true if $a is less than $b |
> | Greater than | $a > $b | true if $a is greater than $b |
<= | Less than or equal to | $a <= $b | true if $a is less than or equal to $b |
>= | Greater than or equal to | $a >= $b | true if $a is greater than or equal to $b |
<?php
$a = 10;
$b = 20;
$c = 10;
// Examples
echo ($a == $c) ? "true" : "false";
echo ($a < $b) ? "true" : "false";
?>
Logical Operators
Operator |
Name |
Example |
Result |
&& | And | $a && $b | true if both $a and $b are true |
|| | Or | $a || $b | true if either $a or $b is true |
! | Not | !$a | true if $a is not true |
Assignment Operators
Operator |
Name |
Example |
Equivalent To |
= | Assignment | $a = $b | $a = $b |
+= | Add and assign | $a += $b | $a = $a + $b |
<?php
$a = 10;
$a += 5;
echo $a;
?>
Conditional (Ternary) Operator
Shorthand for an if-else statement:
$result = (condition) ? value_if_true : value_if_false;
<?php
$age = 18;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status;
?>
String Operators
Operator |
Name |
Example |
Result |
. | Concatenation | $a . $b | Concatenates $a and $b |
.= | Concatenate and assign | $a .= $b | Appends $b to $a |
<?php
$message = "Hello";
$message .= " World!";
echo $message;
?>