9

I think this is a dumb question but I could not find it on php. Why is a + with the = in the following code:

function calculateRanking()
{
    $created = $this->getCreated();

    $diff = $this->getTimeDifference($created, date('F d, Y h:i:s A'));

    $time = $diff['days'] * 24;
    $time += $diff['hours'];
    $time += ($diff['minutes'] / 60);
    $time += (($diff['seconds'] / 60)/60);

    $base = $time + 2;        

    $this->ranking = ($this->points - 1) / pow($base, 1.5);

    $this->save();
}

Is this so $time has all those values or rather it is adding all the values to $time?

Thanks

Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
jcslzr
  • 435
  • 9
  • 18

11 Answers11

36

It's adding all those values to time.

something += somethingelse

is a shortcut for

something = something + somethingelse

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 4
    PLEASE KEEP IN MIND: This doesn't work for strings. To concatenate strings, use .= ... This has driven me nuts! – Nick Stinemates Feb 12 '09 at 20:14
  • That's true, but that's because period is the concatenation operator. + allows concatenation for convenience, but it's really period. – Adam Davis Feb 12 '09 at 20:32
  • why do other people's comments get posted up higher than mine, even though I answered first? Kinda hard to get credit for a correct answer if mine is listed after other people with 1000 times the reputation that I do, even though mine was correct and should be credited with the correct answer. – DMCS Feb 12 '09 at 20:54
  • @dominicminicoopers - Sometimes that just how it goes. Some people might just like the way he phrased it better. He formatted his code as `code`, so maybe people just like that. FWIW, Paul Tomblin (15.4k rep) answered before Adam and he only got 4 up votes. He provided historical context, too. – Tyson Feb 12 '09 at 21:32
11
$time += $diff['hours'];

is the same as saying

$time = $time + $diff['hours'];
Taz
  • 3,718
  • 2
  • 37
  • 59
DMCS
  • 31,720
  • 14
  • 71
  • 104
  • why do other people's comments get posted up higher than mine, even though I answered first? Kinda hard to get credit for a correct answer if mine is listed after other people with 1000 times the reputation that I do, even though mine was correct and should be credited with the correct answer. – DMCS Feb 12 '09 at 20:54
  • If an answer is accepted, it will show first. Past that users will see answers based on the sort order they have selected (oldest|newest|votes). I believe sorting by votes is the default. Your answer is oldest, and ok, but Adam's is more clear. – Zoredache Feb 12 '09 at 21:06
  • Sorry. My answer was always below your even when we had the same vote value. Someone voted my answer up just one above yours and only then was mine listed first. Later the question asker accepted my answer, which puts it at the top. – Adam Davis Feb 12 '09 at 21:43
  • My answers don't get any special treatment due to my reputation. I suspect the reason people liked mine above yours was I took out all the extraneous stuff ($, [], etc) and focused on just the primary issue. My answer isn't better, but it did appeal to more people. – Adam Davis Feb 12 '09 at 21:45
7

a += 2; is the equivalent of a = a + 2;

At one time with some languages (especially very old C compilers), the compiler produced better code with the first option. It sticks around now because it's a common idiom and people used to it think it's clearer.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • It's faster written, expecially with long variable names. That's why it's still used. Same applies for i++, which is again a shortcut for i+=1. Only one character saved, but much easier to write because there are only two (instead of three) DIFFERENT characters. – Bodo Thiesen Feb 12 '09 at 20:07
  • I use it, and I've never thought about it before, but it seems like "faster to type" is sometimes a bad optimization to make - it's a slippery slope back to one letter variable names and other techniques of the last century. – Paul Tomblin Feb 12 '09 at 20:20
  • Thing is, "a = a + 1;" vs. "a += 1;" or "++a;" matters little. However, "long_and_descriptive_array_name [descriptive_function(other_variable)] += 1;" is a win. – David Thornley Feb 12 '09 at 20:26
  • 1
    'faster to type' generally implies 'less likly to have a typo in the code somewhere'. When dealing with PHP, this is especially important. – Sean McSomething Feb 12 '09 at 22:32
  • It used to be that `a = a + 1` meant having to generate separate addresses (lvalue to write to, and rvalue to read from). Modern optimizing compilers probably don't end up wasting so much time and space on redundant address handling, but it's still a nice little shortcut that also makes it clearer what's going on (especially in cases such as David pointed out). As for a++ or ++a, this was done (for C) because the hardware had an increment operator which was faster than loading a, adding 1, and storing back to a. – Phil Perry Aug 21 '13 at 17:19
4

There are lots of these shorthand operators in C, C++ in other modern languages.

a -= b;   // a = a - b;
a *= b;   // a = a * b;
a &= b;   // a = a & b;

etc., etc., etc.

Die in Sente
  • 9,546
  • 3
  • 35
  • 41
3

I just wanted to add that this information is, indeed, on PHP's website in the section on operators, or more specifically, assignment operators.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
2

x += 10 is simply a shorter way to write x = x + 10.

In this case, the code is finding the time difference in hours from a time difference structure.

Jesse Weigert
  • 4,714
  • 5
  • 28
  • 37
2

Shortcut operator for $val = $val + $otherval.

This only works on numeric values

John T
  • 23,735
  • 11
  • 56
  • 82
  • If the 'plus' operator is defined for string operations (e.g., Javascript), val += string will work there. In PHP, it would be `$val .= 'some string'` for concatenation. – Phil Perry Aug 21 '13 at 17:23
2

Let's replace a few things to make it a bit easier to understand.

The += is just the same as below:

$time = $diff['days'] * 24;
$time = $time + $diff['hours'];
$time = $time + ($diff['minutes'] / 60);
$time = $time + (($diff['seconds'] / 60)/60);
MJ.
  • 139
  • 1
  • 2
  • 16
0

Also, "a += b" is an expression who's value can be used again immediately,

(((((a += b) *= c) += d) * e) += f);

is a lot less typing than

a = a + b;
a = a * c;
a = a + d;
a = a * e;
a = a + f;
Die in Sente
  • 9,546
  • 3
  • 35
  • 41
  • 2
    It's not really a lot less typing in your particular example. However, it certainly is a lot less readable. – Konrad Rudolph Feb 12 '09 at 20:15
  • 1
    But a lot more typing that a = (((((a + b) * c) + d) * e) + f) – Paul Tomblin Feb 12 '09 at 20:22
  • Don't write code like this you can't maintain or debug it. Keep it simple, your 5 lines of code will win in the long run... (and execution time will be the same anyway) – Johan Feb 12 '09 at 20:22
  • @Johan: 'win' what? Of course, assuming a reasonable compiler -- which is not always true! -- the two versions will generate the same execution code. Therefore it's a matter of style, not substance. If someone in an interview claimed they couldn't maintain or debug either one I wouldn't hire them. – Die in Sente Feb 13 '09 at 03:49
0

If both operands are arrays, $a += $b is also a shorthand for array_merge($a, $b). This function combines two arrays into one, discarding any key in $b that already exists in $a.

$arr1 = array(4 => "four", 1 => "one", 2 => "two");
$arr2 = array("three", "four", "five");
$arr1 += $arr2;
print_r ($arr1);

// outputs:
Array
(
    [4] => four
    [1] => one
    [2] => two
    [0] => three
)

$arr1 = array(4 => "four", 1 => "one", 2 => "two");
$arr2 = array("three", "four", "five");
$arr2 += $arr1;
print_r ($arr2);

// outputs:
Array
(
    [0] => three
    [1] => four
    [2] => five
    [4] => four
)
Erich
  • 2,408
  • 18
  • 40
0

According to your code

$time += $diff['hours'];
$time += ($diff['minutes'] / 60);
$time += (($diff['seconds'] / 60)/60);

are similar to the following:-

$time = $time + $diff['hours'];
$time = $time + ($diff['minutes'] / 60);
$time = $time + (($diff['seconds'] / 60)/60);
Sheetal Mehra
  • 478
  • 3
  • 13