Welcome to The Coding College! In this tutorial, we’ll explore PHP Operators, one of the building blocks of any programming language. Operators in PHP are used to perform various operations on variables and values, such as arithmetic, comparison, logical operations, and more.
Understanding how PHP operators work will help you write efficient and error-free code.
What Are Operators?
In PHP, operators are symbols or keywords that perform operations on variables or values. For example:
<?php
$a = 5 + 3; // "+" is an operator
?>
Here, +
is an arithmetic operator used to add two numbers.
Types of PHP Operators
PHP provides a variety of operators for different purposes. Let’s explore each type with examples.
1. Arithmetic Operators
Arithmetic operators are used for performing basic mathematical operations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 6 / 3 | 2 |
% | Modulus | 5 % 3 | 2 |
** | Exponentiation | 2 ** 3 | 8 |
Example:
<?php
$x = 10;
$y = 3;
echo $x + $y; // Outputs: 13
echo $x % $y; // Outputs: 1
?>
2. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Assign | $x = 5 | $x = 5 |
+= | Add and assign | $x += 3 | $x = $x + 3 |
-= | Subtract and assign | $x -= 2 | $x = $x - 2 |
*= | Multiply and assign | $x *= 2 | $x = $x * 2 |
/= | Divide and assign | $x /= 2 | $x = $x / 2 |
%= | Modulus and assign | $x %= 2 | $x = $x % 2 |
Example:
<?php
$x = 10;
$x += 5; // $x = $x + 5
echo $x; // Outputs: 15
?>
3. Comparison Operators
Comparison operators are used to compare two values.
Operator | Description | Example | Output |
---|---|---|---|
== | Equal | 5 == 5 | true |
=== | Identical (equal and same type) | 5 === '5' | false |
!= | Not equal | 5 != 3 | true |
!== | Not identical | 5 !== '5' | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 3 <= 5 | true |
Example:
<?php
$x = 5;
$y = "5";
echo $x === $y; // Outputs: false (different types)
?>
4. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
Example:
<?php
$x = 5;
$y = 10;
if ($x > 0 && $y > 0) {
echo "Both are positive numbers.";
}
?>
5. String Operators
String operators are used to manipulate text.
Operator | Description | Example | Output |
---|---|---|---|
. | Concatenation | "Hello " . "World" | Hello World |
.= | Concatenation and assign | $x .= "World" | HelloWorld |
Example:
<?php
$txt = "Hello";
$txt .= " World!";
echo $txt; // Outputs: Hello World!
?>
6. Bitwise Operators
Bitwise operators perform operations on binary numbers.
Operator | Description | Example | Output |
---|---|---|---|
& | AND | 5 & 3 | 1 |
` | ` | OR | `5 |
^ | XOR | 5 ^ 3 | 6 |
~ | NOT | ~5 | -6 |
<< | Left shift | 5 << 1 | 10 |
>> | Right shift | 5 >> 1 | 2 |
7. Increment/Decrement Operators
These operators increase or decrease a variable’s value by one.
Operator | Description | Example | Output |
---|---|---|---|
++$x | Pre-increment | ++$x | 6 |
$x++ | Post-increment | $x++ | 5 |
--$x | Pre-decrement | --$x | 4 |
$x-- | Post-decrement | $x-- | 5 |
8. Array Operators
Array operators are used to compare and manipulate arrays.
Operator | Description | Example | Output |
---|---|---|---|
+ | Union of arrays | $a + $b | Combines arrays |
== | Equality | $a == $b | true or false |
=== | Identity | $a === $b | true or false |
!= | Inequality | $a != $b | true or false |
9. Advanced Operators
1. Null Coalescing (??
)
Returns the first value that exists and is not null
.
Example:
<?php
$x = null;
$y = $x ?? "Default";
echo $y; // Outputs: Default
?>
2. Spaceship Operator (<=>
)
Performs a three-way comparison.
Example | Output |
---|---|
5 <=> 3 | 1 |
5 <=> 5 | 0 |
3 <=> 5 | -1 |
Operator Precedence
Operator precedence determines the order in which operators are evaluated.
Operator | Associativity |
---|---|
() | Left-to-right |
! | Right-to-left |
* , / , % | Left-to-right |
| | +
, -
| Left-to-right | | <
, >
, <=
, >=
| Left-to-right |
Use parentheses ()
to ensure the desired order of execution.
Conclusion
PHP operators are essential for performing calculations, comparisons, and logical operations. By mastering these operators, you can write efficient, readable, and maintainable code.
For more tutorials like this, visit The Coding College and enhance your PHP skills today!