PHP Shorthand Operators

My Hands Aren’t Short!

PHP shorthand operators are amazing! There, I said it. The shorthand operator combines the expression on the right with the assignment operator. The variable that appears on the left-side of the assignment operator needs to appear on the right-hand-side as well in order for you to be able to use the shorthand notation.

Let’s take a look at the following code:

 

<?php$x = 1;
$x = $x + 1;echo $x;?>

 

  1. PHP assigns the integer value 1 to the variable $x.
  2. In the second statement, $x + 1 is evaluated. $x contains the value 1, so the result will be 1 + 1, which equals 2.
  3. The value 2 is then assigned to $x.

If you echo out $x, 2 will be displayed.

Since the variable $x appears on both sides of the assignment operator, we can use the shorthand operator to shorten the expression $x = $x + 1. You just remove the $x from the right hand side and move the + operator in front of the = operator: $x += 1.

 

<?php// Long approach
$x = $x + 1;// Can be shortened to
$x += 1;?>

 

Shorthand operations are not limited to integers; you can use the concatenation shorthand operator to combine strings.

 

<?php// Before
$msg = "Hey";
$msg = $msg . " there";// After
$msg  = "Hey";
$msg .= " there";?>

 

We can apply the same logic to the subtraction, multiplication and even the modulus operators.

 

<?php$x = 0;// Same as $x = $x + 4;
// $x = 0 + 4; 
// $x = 4
$x += 4;// Same as $x = $x - 2;
// $x = 4 - 2;
// $x = 2;
$x -= 2;// Same as $x = $x * 2;
// $x = 2 * 2;
// $x = 4;
$x *= 2;// Same as $x = $x % 2;
// $x = 4 % 2;
// $x = 0;
$x %= 2;?>

 

Shorthand operators are used frequently throughout programming; they’re so frequent that it’s actually rare that you’ll see the long approach in the wild. There are operations that are so frequently used that even shorter operators have been created. I’m talking about the increment and decrement operators, which we covered recently.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Array Operators

How Many Operators do You Want Already?

PHP – P21: Array Operators

PHP Array operators are operators that are used on arrays. Who would have guessed? The first operator that we’ll look at is the + operator. You would think that this operator would be used to merge two arrays, taking elements from both arrays and merging them into a new array, but that is not the case.

PHP Shorthand Operators

My Hands Aren’t Short!

PHP – P22: Shorthand Operators

PHP shorthand operators are amazing! There, I said it. The shorthand operator combines the expression on the right with the assignment operator.

Dino Cajic describing PHP Operand Precedence

WHICH OPERATORS COME FIRST?

PHP – P23: OPERATOR PRECEDENCE

PHP Operator precedence just looks at which operation will be performed first. You’ve seen operator precedence in mathematics. The multiplication and division operators are executed before the addition and subtraction operators. With all of the different operators that we have in PHP, the interpreter needs to know which operation to perform in what order.

Leave a Reply