x+=y is the same as x=x+y??
and
x*=y is the same as x=x*y??
:injured:
x+=y is the same as x=x+y??
and
x*=y is the same as x=x*y??
:injured:
Yep.
Basically:
$X = 1;
$Y = 2;
$A = $X; //a = 1
$A *= $Y; //a = 1 x 2 = 2
$A += $X; //a = 2 + 1 = 3
Basically its a shortcut. $X #= $Y is equivalent to $X = $X # $Y, where # is your given assignment operator, e.g. +, -, /, *.
I believe it came from the increment operator: ++. It simple adds one:
$i = 0;
$i++; //i = 1;
$i++; //i = 2;
$i++; //i = 3;
It’s useful in loops, for example:
for($i = 0; $i < 10; $i++){
echo $i . '<br />';
}
Hello Jake,
Thanks for replying, your post seems very comprehensive. However, I am still confused a little bit.
What exactly is +=?
I am learning from w3schools, and they began this subject by telling me right away that n+=m is the same as n=n+m
perhaps it’s a bad resource to learn this particular topic
+= is just an operator meaning ‘increase by’.
Oh, they should say so in the tutorial lol
So n+=m is actually saying "change the value of ‘n’ by adding the value of ‘m’ to it?
Or,
$x = 2;
$y = 4;
$x *= $y // this says "change the value of 'x' by multiplying the value of 'y' to it"?
Basically, yes
Well, thank you sir.
I like your quote btw, “He who asks is a fool for five minutes. But he who doesn’t remains a fool forever.”
Any time
And the quote, I find it reflects the forums well. Those who ask questions become experts later in life - those who think they understand everything and never ask questions, those people never progress and stay beginners forever.
[ot]> those who think they understand everything and never ask questions, those people never progress and stay beginners forever.
I took a look at the number of questions I’ve asked here… Aww snap. :injured:[/ot]
Something of this sort should be SP’s slogan
:L Well, it’s the REAL experts - the level above - who google things before asking questions
However, we have a friendly community with a lot of patience, to help those who come here as a first call of action.
[ot]Everyone has those newcomer questions at one point, even if they didn’t ask them here. Since I learned everything I know about programming from crawling the internets (and untold hours of practice), I find I get most out of forums like these by simply helping others who stumble along the way.
You bring up a valid point about Google, though. The ability to do quality research on my own has been just as valuable as programming.[/ot]
@OP: if you’ve had experience with lower-level programming, it also might be helpful to think of assignment operators as analogous to certain assembly instructions:
; Add 5 to the value stored in the accumulator, then
; store the result back into it.
add eax, 5 ; $foo += 5
; Shift lowest bit off, storing result back into eax
shr eax, 1 ; $foo >>= 1
Remember not to look at assignment expressions like a = a + b algebraically, since “=” doesn’t mean equal to.
PHP is my first programming language. I don’t understand what you are saying (as yet) but it will probably make sense in near future. Thanks for replying in any case