Operators in PHP

(Last Updated On: December 8, 2018)

1. Introduction

Operators allow us to perform certain operations with values, arrays, and variables. There are several different types of operators. In PHP 7, two new operators were introduced – null coalescing and spaceship operator. We have covered them as well. Let’s see different types of operators:

2. Arithmetic Operators

+ — Addition

– — Subtraction

* — Multiplication

/ — Division

% — Modulo (the remainder of the value divided by another)

** — Exponentiation

Example:

$a = 5;
$b = 2;

$c = $a + $b;
echo $c; //prints 7

$c = $a - $b;
echo $c; //prints 3

$c = $a * $b;
echo $c; //prints 10

$c = $a / $b;
echo $c; //prints 2.5

$c = $a % $b;
echo $c; //prints 1

$c = $a ** $b;
echo $c; //prints 25

3. Assignment Operators

Besides the standard assignment operator (=), we also have the following options:

+= — a += b is the same as a = a + b

-= — a -= b is the same as a = a – b

*= — a *= b is the same as a = a * b

/= — a /= b is the same as a = a / b

%= — a %= b is the same as a = a % b

4. Comparison Operators

== — Equal

=== — Identical

!= — Not equal

<> — Not equal

!== — Not identical

< — Less than

> — Greater than

<= — Less than or equal to

>= — Greater than or equal to

<=> — Less than, equal to, or greater than

$a = 5;
$b = "5";

var_dump($a == $b); // true
var_dump($a === $b); // false

5. Logical Operators

and — And

or — Or

xor — Exclusive or

! — Not

&& — And

|| — Or

6. Bitwise Operators

& — And

| — Or (inclusive or)

^ — Xor (exclusive or)

~ — Not

<< — Shift left

>> — Shift right

7. Error Control Operator

We can use the @ sign to prevent expressions from generating error messages. This is often important for security reasons, for example, to keep confidential information safe.

8. Increment/Decrements Operators

++$v — Increments a variable by one, then returns it

$v++ — Returns a variable, then increments it by one

–$v — Decrements the variable by one, returns it afterward

$v– — Returns the variable then decrements it by one

9. String Operators

. — Used to concatenate (mean combine) arguments

.= — Used to append the argument on the right to the left-side argument

10. The null coalescing operator

It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand.

Consider this example:

// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';

11. The spaceship operator

The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

12. Conclusion

In this post, we are looking at different operators available to us. In the next post in this series, we will discuss how we can put conditional logic in our programs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here