Dec
12
2008

PHP wish list – part 2: reverse combined operators

It’s time for part 2 of my PHP wish list! In this instalment I’ll detail another feature that I would love to see in PHP: reverse combined operators.

If you missed part 1 (you probably did), you can read it here.

“What the hell is a reverse combined operator?” I hear you scream. Well, technically they probably don’t exist yet, so I’m sure a better name could be found, but bear with me and I’ll try to explain…

If you’ve ever done any programming, then you’ve almost certainly used operators whether you realise it or not. Basically, they are some of the most basic building blocks of a programming language, things like +, -, = and == are all operators. For a more detailed description, see the PHP manual.

One of the most commonly used operators in any language is the assignment operator (=). This operator is used to assign a value to a variable. Here is a simple example:

$variable = 1 + 1;

echo $variable; // outputs "2"

In the above example, PHP first uses the addition operator to get the sum of 1 and 1. It then stores the result in $variable using the assignment operator.

Now consider the following situation; you have a variable that you want to add a certain amount to. Rather than writing the whole expression out like this:

$variable = $variable + 1;

PHP allows you to shorten the above to just:

$variable += 1;

This is called a combined operator because it is effectively a combination of the + and = operators. This combining of operators is only ever done with the assignment operator and one other operator. Here are all the combined operators that I am aware of:

Operator Equivalent to Description
$a += $b $a = $a + $b Addition / array union
$a -= $b $a = $a – $b Subtraction
$a *= $b $a = $a * $b Multiplication
$a /= $b $a = $a / $b Division
$a %= $b $a = $a % $b Modulus
$a .= $b $a = $a . $b Concatenation
$a &= $b $a = $a & $b Bitwise and
$a |= $b $a = $a | $b Bitwise or
$a ^= $b $a = $a ^ $b Bitwise xor
$a <<= $b $a = $a << $b Left shift
$a >>= $b $a = $a >> $b Right shift

What I would like to see are reverse combined operators. Essentially these would be the same as regular combined operators, except with the order of operands reversed. This would produce the following options:

Operator Equivalent to Description
$a =+ $b $a = $b + $a Array union
$a =- $b $a = $b – $a Subtraction
$a =/ $b $a = $b / $a Division
$a =% $b $a = $b % $a Modulus
$a =. $b $a = $b . $a Concatenation
$a =<< $b $a = $b << $a Left shift
$a =>> $b $a = $b >> $a Right shift

Note that not every combined operator would imply the need for a reverse version because the order of operands has no effect on the result in some cases.

I’ll now provide you with a couple of examples where something like this could come in handy.

Inverting a number:

$v = 100;
$v =/ 1;

echo $v; // would output "0.01"

Controlling which array takes precedence when merging arrays using the array union operator (basically just the + operator, but when used on arrays you get the union of the two):

$v = array(0 => 'one', 1 => 'two', 2 => 'three');
$v += array(1 => 'apple', 2 => 'banana', 3 => 'orange');

for ($i = 0; $i &lt; count($v); $i ++)
{
  echo $v[$i].',';
}

// this would output "one,two,three,orange,"

$v = array(0 => 'one', 1 => 'two', 2 => 'three');
$v =+ array(1 => 'apple', 2 => 'banana', 3 => 'orange');

for ($i = 0; $i &lt; count($v); $i ++)
{
  echo $v[$i].',';
}

// this would output "one,apple,banana,orange,"

I’m sure if you racked your brain you could come up with dozens of other handy little uses for this feature. Let me know if you can think of any others!

Written by Erin in: PHP,Programming | Tags: , , ,

6 Comments »

  • Dmoney says:

    Yeh i didn’t understand one single syllable on this page…however i do feel smarter…

  • Stuart Jones says:

    Actually, I’m torn on this one – I do like what you have done, and it makes sense… But, again, it goes against the principles of simplicity – combined operators obfuscate a teensy amount, and having += and =+ could easily become confusing – but they are very useful.

    Do you know of any other languages that use a similar syntax? I’m pretty sure you got the foreachif idea from smarty…

  • Erin says:

    Simplicity is a point to consider, but there’s nothing forcing you to use combined operators or other syntactic sugar either. The choice of simplicity is always in the hands of the developer.

    Consider also the fact that people at a skill likely to confuse += with =+ would most likely be using it for integer addition, in which case there is no issue because the result would be the same.

    No, actually, I don’t know of any languages that support a similar syntax. Find me one and I’ll try it! :)

    I’ve actually never seen anything like the foreachif idea either. Smarty supports a foreachelse which works like an else statement on a foreach loop, and someone came up with another usage for the term foreachif (you’ll find it if you Google) but neither of those are the same concept.

  • Ezzatron » PHP wish list – part 2: reverse combined operators…

    “It’s time for part 2 of my PHP wish list! In this instalment I’ll detail another feature that I would love to see in PHP: reverse combined operators.”…

  • JediLlama says:

    I’m not sure about this. It’s a nice little bit of shorthand, but it’s really just at the expense of readability. Just to save a few keystrokes ;) This is the kind of thing that happened to perl…lots of shortcuts and other tricks turned the language into an unreadable mess…

  • Elliott says:

    This, on the other hand, I could see the use of.

    It does seem like it would cause a lot of heartache and debugging trouble, though. A slip of the fingers could easily lead to a very hard to find divide by zero error.

RSS feed for comments on this post. TrackBack URL


Leave a Reply