PHP Operators. In-depth Look At What You Need To Know

Posted by TotalDC

Now that we covered strings in PHP and some built-in functions to manipulate strings, let’s talk about all kinds of operators that are available in PHP.

What Is Operators In PHP

Operators are symbols that tell the PHP to perform certain actions. For example the addition (+) symbol is an operator that tells PHP to add two variables or values. While The greater-than (>) symbol is an operator that tells PHP to compare two values.

Here is a list describing the different operators used in PHP.

PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetical operations like addition, subtraction, multiplication and many more. Here is a complete list of arithmetic operators in PHP.

<?php
$x = 16;
$y = 4;
print($x + $y); // Result: 20
print($x - $y); // Result: 12
print($x * $y); // Result: 64
print($x / $y); // Result: 4
print($x % $y); // Result: 0
?>

PHP Assignment Operators

The assignment operators are used to assign values to variables.

<?php
$x = 10;
print $x; // Result: 10
 
$x = 20;
$x += 30;
print $x; // Result: 50
 
$x = 50;
$x -= 20;
print $x; // Result: 30
 
$x = 5;
$x *= 25;
print $x; // Result: 125
 
$x = 50;
$x /= 10;
print $x; // Result: 5
 
$x = 100;
$x %= 15;
print $x; // Result: 10
?>

PHP Comparison Operators

The comparison operators are used to compare two values and return Boolean (true or false). The following example will show you these comparison operators in action:

<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z);  // Result: boolean true
var_dump($x === $z); // Result: boolean false
var_dump($x != $y);  // Result: boolean true
var_dump($x !== $z); // Result: boolean true
var_dump($x < $y);   // Result: boolean true
var_dump($x > $y);   // Result: boolean false
var_dump($x <= $y);  // Result: boolean true
var_dump($x >= $y);  // Result: boolean false
?>

PHP Incrementing And Decrementing Operators

The increment/decrement operators are used to increment or decrement a variable’s value and the following example will show you these increment and decrement operators in action:

<?php
$x = 10;
print ++$x; // Result: 11
print $x;   // Result: 11
 
$x = 10;
print $x++; // Result: 10
print $x;   // Result: 11
 
$x = 10;
print --$x; // Result: 9
print $x;   // Result: 9
 
$x = 10;
print $x--; // Result: 10
print $x;   // Result: 9
?>

PHP Logical Operators

The logical operators are typically used to combine conditional statements. Let’s look at some examples:

<?php
$year = 2021;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    print "$year is a leap year.";
} else{
    print "$year is not a leap year.";
}
?>

PHP String Operators

There are two operators which are designed forĀ strings. Here is an example for you:

<?php
$x = "Hello";
$y = " World!";
print $x . $y; // result: Hello World!
 
$x .= $y;
print $x; // result: Hello World!
?>

PHP Array Operators

The array operators are used to compare arrays. The following example will show you these array operators in action:

<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Black", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y);   // result: boolean false
var_dump($x === $y);  // result: boolean false
var_dump($x != $y);   // result: boolean true
var_dump($x <> $y);   // result: boolean true
var_dump($x !== $y);  // result: boolean true
?>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: