Table of Contents:
PHP Operators Explained | Types of Operators in PHP with Examples
In PHP, operators are essential tools that allow developers to perform calculations, make comparisons, manipulate strings, and control program flow. Every PHP script uses operators in some way, making them a vital concept to master for efficient coding.
This complete guide explains types of PHP operators with examples, helping you understand their behavior and application in real projects.
What Are PHP Operators and How They Work in Programming?
Operators in PHP are symbols or keywords that tell the interpreter to perform a specific operation. These operations may involve numbers, strings, or variables.
Example:
<?php
$x = 10;
$y = 5;
echo $x + $y; // Output: 15
?>
Here, the +
sign is an arithmetic operator used to add two values.
Arithmetic Operators in PHP with Syntax and Examples
Arithmetic operators are used for mathematical operations like addition, subtraction, multiplication, and division.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $x + $y | Adds two numbers |
– | Subtraction | $x - $y | Subtracts $y from $x |
* | Multiplication | $x * $y | Multiplies two numbers |
/ | Division | $x / $y | Divides $x by $y |
% | Modulus | $x % $y | Remainder of division |
** | Exponentiation | $x ** $y | Power of $x to $y |
Example:
<?php
$a = 8;
$b = 2;
echo $a ** $b; // Output: 64
?>
PHP Assignment Operators Explained with Examples
Assignment operators are used to assign or modify variable values. The =
operator is basic, but PHP supports compound assignments for concise code.
Operator | Example | Equivalent |
---|---|---|
= | $x = $y | Assigns $y to $x |
+= | $x += $y | Adds and assigns |
-= | $x -= $y | Subtracts and assigns |
*= | $x *= $y | Multiplies and assigns |
/= | $x /= $y | Divides and assigns |
%= | $x %= $y | Finds remainder and assigns |
Example:
<?php
$total = 100;
$total += 50;
echo $total; // Output: 150
?>
Comparison Operators in PHP for Conditional Statements
Comparison operators are used to compare two values and return true
or false
depending on the result.
Operator | Description | Example |
---|---|---|
== | Equal | $x == $y |
=== | Identical (value & type) | $x === $y |
!= | Not equal | $x != $y |
<> | Not equal | $x <> $y |
!== | Not identical | $x !== $y |
> | Greater than | $x > $y |
< | Less than | $x < $y |
>= | Greater or equal | $x >= $y |
<= | Less or equal | $x <= $y |
<=> | Spaceship operator | $x <=> $y |
Example:
<?php
$a = 5;
$b = 10;
var_dump($a <=> $b); // Output: int(-1)
?>
PHP Logical Operators with Examples for Conditional Expressions
Logical operators combine multiple conditions to control program flow.
Operator | Name | Example | Description |
---|---|---|---|
&& | And | $x && $y | True if both are true |
|| | Or | `$x | |
! | Not | !$x | True if $x is false |
and | And | $x and $y | Same as && but lower precedence |
or | Or | $x or $y | Same as || but lower precedence |
xor | Exclusive Or | $x xor $y | True if only one is true |
Example:
<?php
$isAdmin = true;
$isLoggedIn = true;
if ($isAdmin && $isLoggedIn) {
echo "Access granted!";
}
?>
String Concatenation Operators in PHP with Examples
String operators are used to join or append strings.
Operator | Name | Example | Description |
---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Joins two strings |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends to existing string |
Example:
<?php
$text = "PHP ";
$text .= "Operators";
echo $text; // Output: PHP Operators
?>
PHP Increment and Decrement Operators Examples
Used to increase or decrease variable values by one.
Operator | Name | Example | Description |
---|---|---|---|
++$x | Pre-increment | Increments before returning | |
$x++ | Post-increment | Increments after returning | |
–$x | Pre-decrement | Decrements before returning | |
$x– | Post-decrement | Decrements after returning |
Example:
<?php
$num = 5;
echo ++$num; // Output: 6
?>
Array Operators in PHP for Comparing and Merging Arrays
Array operators compare and combine arrays.
Operator | Name | Description |
---|---|---|
+ | Union | Combines arrays |
== | Equality | True if same key/value pairs |
=== | Identity | True if identical type and order |
!= | Inequality | True if not equal |
<> | Not equal | Alternative form |
!== | Non-identity | True if not identical |
Example:
<?php
$a = ["a" => "red", "b" => "green"];
$b = ["c" => "blue"];
$result = $a + $b;
print_r($result);
?>
Operator Precedence and Associativity in PHP
Operator precedence determines the order of execution when multiple operators are used. Associativity defines the direction (left-to-right or right-to-left).
Example:
<?php
echo 5 + 3 * 2; // Output: 11
?>
Multiplication is executed before addition because it has higher precedence.
PHP operators, types of operators in PHP, PHP arithmetic operators, comparison operators in PHP, logical operators PHP, PHP operator examples, PHP tutorial, PHP programming
👉 Deep dive into PHP Conditional Statements to use operators in logic.
👉 Learn about PHP Variables and how they interact with operators.
👉 Explore more in PHP Tutorials for Beginners to build your programming foundation.
Frequently Asked Questions (FAQ)
What are PHP operators?
PHP operators are symbols that perform actions on variables or values, like addition, comparison, or logical checks.
What’s the difference between == and === in PHP?
==
checks if values are equal, while ===
checks both value and data type.
What is the use of the spaceship operator (<=>) in PHP?
It compares two expressions and returns -1, 0, or 1, showing whether one value is smaller, equal, or greater.
What does .= operator do?
It appends one string to another, updating the original variable.
How does precedence affect operator behavior?
Operators with higher precedence execute first; for example, multiplication happens before addition.