-1

I've been looking through a few mock exams and study guides for PHP and I came across this example:

$a = array(1, 2, 3);
foreach ($a as $x)
    $x *= 2;
echo $a[0]  * $a[1]  * $a[2];

This returns 6.

My question is: what exactly does the *= operator mean?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • 3
    [What does this symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Mark Baker Jun 27 '13 at 09:45
  • 2
    @MarkBaker Useful question, but `*=` isn't mentioned there. – Danny Beckett Jun 27 '13 at 09:47
  • This example is really really bad as a teaching tool (it's either misguided or trying to show that non-reference `foreach` does not make changes stick -- in which case, why confuse the student with extra multiplications after the loop that have nothing to do with making that point?). Beware of that study guide. – Jon Jun 27 '13 at 09:50
  • 1
    I don't see who uvoted this question. This really doesn't show any research effort at all. – Aleph Jun 27 '13 at 09:51
  • @AnotherTest I challenge you to Google a symbol like this. You won't find anything useful. – Danny Beckett Jun 27 '13 at 09:51
  • `$a*=b$ $a-=$b $a+=$b $a/=$b ....` == ` $a= $a*$b, $a= $a-$b, $a= $a+$b, $a= $a/$b, ` – Robert Jun 27 '13 at 09:52
  • 2
    This is covered in the [PHP language reference](http://www.php.net/manual/en/language.operators.assignment.php): *"In addition to the basic assignment operator, there are "combined operators" for all of the [binary arithmetic](http://www.php.net/manual/en/language.operators.php), array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression."* Though I understand that it might not be easy to find if you don't know what to look for (*assignment* is the keyword here). – Felix Kling Jun 27 '13 at 09:52
  • 1
    @DannyBeckett: Go to Google and start typing `php *=`. You do not even have to know the term "operator". – Jon Jun 27 '13 at 09:52
  • @DannyBeckett Who said anything about google? http://php.net/manual/en/language.operators.assignment.php – Aleph Jun 27 '13 at 09:53
  • The study guide comes from Zend, I wasn't particularly interested in doing the exam I just wanted to have an idea of what kind of knowledge they would expect to pass it. – Philip Richardson Jun 27 '13 at 09:53
  • 1
    @AnotherTest If you're trying to find something out, you'd typically Google it. Searching for something like `php *=` yields utter crap. – Danny Beckett Jun 27 '13 at 09:54
  • 1
    @DannyBeckett If it's something related to the PHP core language, it'd typically search php.net. – Aleph Jun 27 '13 at 09:56
  • @Jon Again, utter crap: [https://www.google.co.uk/search?q=php+*%3D+operator](https://www.google.co.uk/search?q=php+*%3D+operator) – Danny Beckett Jun 27 '13 at 09:56
  • @AnotherTest **Good luck search php.net!** http://uk1.php.net/results.php?q=%2A%3D&l=en&p=all – Danny Beckett Jun 27 '13 at 09:57
  • 1
    @DannyBeckett He said "operator" in the question. So anyone would search for "operator" on the online documentation. – Aleph Jun 27 '13 at 09:58
  • People, my point here is: trying to find out what a symbol does, in any language, inherently takes the mickey. – Danny Beckett Jun 27 '13 at 09:58

4 Answers4

3
$x *= 2; 

is nothing but

$x = $x * 2;
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
3

It's a shortcut for:

$x = $x * 2

A syntax which dates back to C and accounts for natural (from a human's point of view) "multiply $x by 2", rather than programmatic "take $x value, multiply it by 2 and reassign back to $x" -- as Kernighan and Ritchie stated in their The C programming language.

It can be applied to all numerical operators:

$x = $x + 2 <---> $x += 2
$x = $x - 2 <---> $x -= 2
$x = $x / 2 <---> $x /= 2
etc...

By the way, the printed value 6 it's not related.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
1

$x *= 2; is the same as $x = $x * 2; (like += and -= and .= etc)

HamZa
  • 14,671
  • 11
  • 54
  • 75
Imane Fateh
  • 2,418
  • 3
  • 19
  • 23
1

+=, *=, /= and -= are compound operators. In your code, it would expand to:

$x = $x * 2
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
SBI
  • 2,322
  • 1
  • 17
  • 17