Assignment Operators

Last updated on

PHP assignment operators enable you to frequently engage in performing calculations and operations on variables, requiring the assignment of results to other variables. Consequently, this is precisely where assignment operators prove indispensable, allowing you to seamlessly execute an operation and assign the result to a variable within a single statement.

In the following sections, we’ll delve into the different types of PHP assignment operators and explore how to use them.

The most commonly used assignment operator in PHP is the equals sign (=). For instance, in the following code, the value 10 is assigned to the variable $x:

<?php 
$x = 10;

Now, let’s explore each type with examples:

PHP Arithmetic Assignment Operators

Numeric data undergoes mathematical operations through the utilization of arithmetic operators. In PHP, this is where arithmetic assignment operators come into play, employed to perform these operations. The arithmetic assignment operators include:

  • += or $x + $y (Addition Assignment Operator)
  • -= or $x - $y (Subtraction Assignment Operator)
  • *= or $x * $y (Multiplication Assignment Operator)
  • /= or $x / $y (Division Assignment Operator)
  • %= or $x % $y (Modulus Assignment Operator)
  • **= or $x ** $y (Exponentiation Assignment Operator)

Consider the following example:

<?php 
$x = 10;
$x += 5; // equivalent to $x = $x + 5
echo $x; // Output: 15

In this example, the addition assignment operator increases the value of $x by 5, and then assigns the result back to $x, producing an output of 15.

You can leverage these arithmetic assignment operators to perform complex calculations in a single statement, making your code more concise and easier to read. For more details, refer to this tutorial.

One crucial aspect of computer science involves the manipulation of binary bits in PHP. Let’s delve into one of the more complex assignment operators—specifically, the PHP bitwise operators.

PHP Bitwise Assignment Operators

Developers use bitwise operators to manipulate data at the bit level. PHP bitwise assignment operators perform bitwise operations. Here are the bitwise assignment operators:

  • &= or $x & $y (Bitwise AND Assignment Operator)
  • |= or $x | $y (Bitwise OR Assignment Operator)
  • ^= or $x ^ $y (Bitwise XOR Assignment Operator)
  • ~= or $x ~ $y (Bitwise NOT Assignment Operator)
  • <<= or $x << $y (Left Shift Assignment Operator)
  • >>= or $x >> $y (Right Shift Assignment Operator)

Let’s illustrate with an example:

<?php 
$x = 5;
$x |= 2; // equivalent to $x = $x | 2
echo $x; // Output: 7

In this example, a bitwise OR operation is performed between the values of $x and 2 using the bitwise OR assignment operator. The result is then assigned to $x, yielding an output of 7.

Here is a full explanation along with more examples of bitwise operators.

Let’s move to the section below to understand the Null Coalescing Operator in PHP.

Null Coalescing Operator

Furthermore, developers use the null coalescing operator to assign a default value to a variable if it is null. The double question mark (??) symbolizes the null coalescing operator. Consider the following example:

<?php 
$name = null;
$fullName = $name ?? 'John Doe';
echo $fullName; // Output: John Doe

In this example, the null coalescing operator is used to assign the value ‘John Doe’ to the variable $fullName. If $name is null, the value ‘John Doe’ will be assigned to $fullName.

The null coalescing operator simplifies code by enabling the assignment of default values to variables. By reading this tutorial, you will gain more information about it

Anyway, assigning a reference in PHP is one of the language’s benefits, enabling developers to set a value and refer back to it anywhere during the script-writing process. Let’s move to the section below to take a closer look at this concept.

Assigning a Reference to a PHP Variable

Furthermore, individuals use the reference assignment operator =& to assign a reference to a variable rather than copying the value of the variable. Consider the following example:

<?php 
$x = 5;
$y =& $x;
$x = 10;
echo $y; // Output: 10

In this example, a reference to the variable $x is created using the reference assignment operator. The value 10 is then assigned to $x, and the output displays the value of $y. Since $y is a reference to $x, it also changes to 10, resulting in an output of 10.

The reference assignment operator proves useful when working with large data sets, enabling you to avoid copying large amounts of data.

Let’s explore some other assignment operators in the paragraphs below.

Other Assignment Operators

In addition to the arithmetic, bitwise, null coalescing, and reference assignment operators, PHP provides other assignment operators for specific use cases. These operators are:

  • .= (Concatenation Assignment Operator)
  • ??= (Null Coalescing Assignment Operator)

Now, let’s explore each of these operators in turn:

Concatenation Assignment Operator: Used to concatenate a string onto the end of another string. For example:

<?php 
$string1 = 'Hello';
$string2 = ' World!';
$string1 .= $string2; // equivalent to $string1 = $string1 . $string2
echo $string1; // Output: Hello World!

In this example, the concatenation assignment operator appends the value of $string2 to the end of $string1, resulting in the output “Hello World!”.

The Null Coalescing Assignment Operator is employed to assign a default value to a variable when it is detected as null. For example:

<?php 
$name = null;
$name ??= 'John Doe';
echo $name; // Output: John Doe

In this example, the null coalescing assignment operator assigns the value ‘John Doe’ to the variable $name. If $name is null, the value ‘John Doe’ will be assigned to $name.

Let’s summarize it.

Wrapping Up

we’ve taken a comprehensive look at the different types of PHP assignment operators and how to use them. Assignment operators are essential tools for any PHP developer, facilitating calculations and operations on variables while assigning results to other variables in a single statement.

Moreover, leveraging assignment operators allows you to make your code more concise, easier to read, and helps in avoiding common programming errors.

Share on: