9
votes

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

11

11 Answers

36
votes

It's adding all those values to time.

something += somethingelse

is a shortcut for

something = something + somethingelse

-Adam

11
votes
$time += $diff['hours'];

is the same as saying

$time = $time + $diff['hours'];
7
votes

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.

4
votes

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.

2
votes

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.

2
votes

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);
2
votes

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

This only works on numeric values

2
votes

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

1
votes

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;
0
votes

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
)
0
votes

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);