Simplifying Your Code with Advanced Operators in PHP

5 May 2024

Understanding the different operators in PHP can help you write cleaner, more efficient and more effective code, while also helping you becoming a more skilled and versatile developer. Knowing the different operators in PHP is important for several reasons:
  1. Efficiency Understanding how to use the appropriate operators can make your code more efficient by reducing the number of lines of code needed to achieve a specific task.
  2. Readability The use of advanced operators can make your code more concise and easier to read, especially when dealing with complex logic.
  3. Accuracy Certain operators perform specific operations that cannot be achieved using basic arithmetic or logical operators. Knowing how to use these operators can ensure the accuracy and integrity of your code.
  4. Compatibility Different versions of PHP may support different operators. Knowing which operators are available and which versions of PHP they are compatible with can help ensure your code is portable and compatible across different environments.
All available operators can be seen on PHP.net operators. In this post however I'll only cover the more advanced operators. We will take a closer look at the following operators:
Name Syntax Example
Null coalescing ?? $username = $name ?? 'Guest';
Null coalesce assignment ??= $username ??= 'Guest';
Spacehip <=> $result = $a <=> $b;
Ternary ?: $result = ($a > $b) ? 'Greater' : 'Lesser or equal';

Null coalescing operator

The null coalescing operator (??) is a relatively new addition to PHP, having been introduced in version 7.0. It allows you to check if a variable exists and is not null, and if it doesn't exist or is null, it will return a default value.

The null coalescing operator can improve your code like this:
// Without null coalescing.
if (isset($name)) {
	$username = $name;
} else {
	$username = 'Guest';
}

// Using null coalescing operator.
$username = $name ?? 'Guest';
In the example the null coalescing operator is used to assign the value of $name to $username if it exists and is not null. Otherwise the default value of 'Guest' is assigned.

The operator helps making the code more concise.

Null coalesce assignment operator

Next up is the null coalesce assignment operator (??=), which is a combination of the null coalescing operator and the assignment operator (=). It allows you to assign a value to a variable only if it does not already exist or is null.
// Without null coalesce assignment.
if (!isset($username)) {
	$username = 'Guest';
}

// Using null coalesce assignment operator.
$username ??= 'Guest';
In the example the null coalesce assignment operator is used to assign the value of 'Guest' to $username only if it does not already exist or is null.

Spaceship operator

The spaceship operator (<=>) is another operator that was introduced in PHP 7.0. It is used to compare two values and return -1, 0, or 1, depending on whether first value is less than, equal to, or greater than the second value.
// Without spaceship.
if ($a > $b) {
	$result = 1;
} elseif ($a == $b) {
	$result = 0;
} else {
	$result = -1;
}

// Using spaceship operator.
$result = $a <=> $b;
In the example the spaceship operator is used to compare the values of $a and $b and return the appropriate value. The operator makes the code more concise and easier to read.

Ternary operator

The ternary operator (?:) is a shorthand way of writing an if-else statement. It allows you to assign a value to a variable based on a condition.

The syntax of the ternary operator is as follows:
condition ? value_if_true : value_if_false
Here is an example of the ternary operator in use:
// Without ternary. 
if ($a > $b) {
	$result = 'Greater';
} else {
	$result = 'Lesser or equal';
}

// Using ternary operator. 
$result = ($a > $b) ? 'Greater' : 'Lesser or equal';
In the example the ternary operator is used to assign the value of $result based on the condition $a > $b. If the condition is true, the value 'Greater' is assigned, otherwise the value 'Lesser or equal' is assigned.

Ternary shorthand operator (Elvis operator)

The Elvis operator is a shorthand version of the ternary operator. To see the difference, consider the following two statements:
$username = $name ? $name : 'Guest'; // Ternary operator.
$username = $name ?: 'Guest'; // Elvis operator.
Both statements assign $name to the $username variable if $name is set. Otherwise the value 'Guest' is used.

The name is derived from the fact that when viewed sideways, the operator looks like a set of eyes and Elvis' signature haircut.

Wrapping up

So there you have it — an introduction to some of the more advanced operators in PHP. By understanding and mastering these operators, you can write cleaner, more efficient code and take your programming skills to new heights.

You might also enjoy

Getting title tag and meta tags from a website using PHP

Getting title tag and meta tags from a website using PHP

Published 2024-05-14

PHP

Web development

Learn how to easily webscrape title and meta tags from a website using simple PHP commands.

Read the post →
The Seven Restful Controller Actions in Laravel

The Seven Restful Controller Actions in Laravel

Published 2024-05-05 — Updated 2024-05-16

Laravel

PHP

Web development

The seven RESTful actions and how you could implement them in Laravel

Read the post →
Useful Laravel packages

Useful Laravel packages

Published 2024-05-05

Laravel

PHP

Web development

Learn what packages that can help you build even better websites in Laravel. We'll go trhough the must-haves and packages that are just nice to have.

Read the post →