Validity and usage =+ in PHP - php

Take this example:
<?php
$i = 1;
$i += 2;
$i =+ 5;
echo $i; // 5
This shows that =+ is an assignment operator. Still, this is highly confusing to me and not at all semantic. I spent hours debugging something simply because I accidentally used =+ instead of +=. The former does not throw errors, though. So I am curious: what is the use case for =+. When would you ever (need to) use it over a simple =?

=+ is not an operator. Think of it as
$i = +5;
as opposed to
$i = -5;

=+ is not a single operator. Its two: the assignment (=) and the unary plus (+).
If you insert different whitespace it gets obvious:
$i = +5;
The unary plus in PHP is used for conversion of $i to int or float as appropriate. Since 5 already is an int, its the identity in this case, and the whole expression is semantically equivalent to:
$i = 5;

As you mentioned += is an assignment operator is correct.
In your case:
$i = 1;
Just assign the value 1 to $i and then when your second line execute $i += 2;, its just add the value 2 with the existing value of $i.
So now when you use $i =+ 5; in your third line, its re-assign the variable $i with the positive value +5.
There are no big deal with =+ and += these are different.

Related

PHP operator precedence & string concatination?

See the following code snippet
$i=1;
echo $i.($i++);
in a quick, I thought the result would be 12 but the actual result is 21.
also
echo $i,$i++;
I thought it would be 12 but its 11 .
echo ($i = ($i++)); //result is 1
echo ($i = ($i+1)); //result is 2
But why?
When a variable is not involved into any arithmetic operation (like your first $i), PHP will not create a temporary variable.
Thus, your first $i will be evaluated at the end of the statement, when $i++ has already been executed.
To prevent this, you can still write:
echo ($i += 0).($i++);
But this is obviously not a good coding practice.
EDIT: When you use , it is actually syntactic sugar to shorten two PHP statements. It is strictly equivalent to:
echo $i;
echo $i++;
Since incrementation is executed after the last statement, 11 is indeed the result.
First example
Code in brackets is evaluated first - in this case ($i++). The value of $i is taken (1) and then the variable is incremented to 2. So you then have this, where $i is 2.
echo $i . '1'
From this, the value of $i is substituted in, and you get '2' . '1', which is concatenated to give '21'.
Second example
It's easier to rewrite this to clear up the , separator. The line echo $i, $i++; is equivalent to:
echo $i;
echo $i++;
The first line obviously outputs 1, and the second will output that same value, then increment $i (++ is the post-increment operator). If you were to put another echo $i; at the end, it would output 2.
As per the PHP documentation stated at: Operator Precedence
First Case
$i=1;
echo $i.($i++);
$i is initialized to value 1. Now, ++ follows a higer precedence than . and it has right-associative. This means your $i++ will be evaluated first. In this case , the value of $i++ will be 1 and the next value of $i will get incremented to 2. hence $i is 2
Now . has the next precendence after ++, which is left-associative. hence it will evaluate values starting from left.
so $i=2 and $i++ =1, hence the output 21
Second Case
$i=1;
echo $i,$i++;
Here, there is only one operator ++. Hence the need for comparision of precedence doesn't arise. Hence, it will be evalauted by default standard of left-associative. $i = 1, $i++ = 1. Hence 11
Third Case
echo ($i = ($i++)); //result is 1
In this case, now = is an assignment operator and is right-associative, so $i++ = 1. And since it is an assignment operator value of $i++ will be stored in $i. hence echo ($i = 1); which will result in output being 1.
Fourth Case
echo ($i = ($i+1)); //result is 2
Again, this will be right-associative, so $i+1 = 2. hence echo ($i = 2); which will result in output being 2.
Firstly for the second place it uses $i eq 1
Then it increases it to 2;
So for the first place it uses 2 and for the second - 1

Double equals in for loop?

Why doesn't the double equals work in a PHP for loop? The following works:
$cnt = 5;
for ($z = 0; $z <= $cnt; $z++) {
echo $z."!<br />";
}
But why doesn't this work?
$cnt = 5;
for ($z = 0; $z == $cnt; $z++) {
echo $z."!<br />";
}
The loop executes only if the condition evaluates to true. At the first iteration, $z == $cnt is false, so the loop never executes.
A common loop strategy is to use a sentinel value:
$cnt = 5;
$stop = $cnt + 1;
for ($z = 0; $z != $stop; $z++) {
. . .
}
Note that the comparison is negative (!= or !==). Using a sentinel is usually unnecessary for numerical loop variables (I wouldn't recommend it for your posted code), but is useful for other situations (e.g., when $stop represents null, an illegal value, etc.). It's particularly helpful when the loop variable changes in a pattern that is not easy to characterize succinctly.
because on the first iteration $z != $cnt, so the loop stops immediately.
Let us look at this from the computer's perspective:
If I am a computer this is what you told me to do:
set $cnt = 5;
set $z = 0;
check if $z equals $cnt
if so do whatever is in the loop, then increment $z
Trouble is, 5 does not equal 0 and never will, so the loop will simply be skipped. If you had $cnt = $z+1 inside the loop this would be an infinite loop.
So, you see, == works just fine, it simply doesn't do what you think it should do.
Hope this helps!
A for loop works by looping until a condition is made false. In the first example the loop will execute until z = 5 after that z will not longer be less than or equal to cnt (z starts at 0 and increments through each loop). In the second example you have $z == $cnt as your condition, and since z = 0 and cnt = 5 the loop will stop automatically because the condition is made false. You can use not equals instead like following:
$cnt = 6;
for ($z = 0; $z != $cnt; $z++) {
echo $z."!<br />";
}
The syntax of for is:
for (<init>; <condition>; <increment>)
The loop tests <condition> before each iteration. If it's true, it executes that iteration; if it's false, the loop terminates.
In your case, since $z == $cnt is false before the first iteration, the loop terminates immediately.
To do what you want, invert the test. You also need to bump the end value up by one, since the original version used <=, not <. Note that in both cases, the loop executes $cnt+1 times, because you start from 0.
for ($z = 0; $z != $cnt+1; $z++)

Variable merging which doesn't make sense

Consider this code:
$x = 1.4;
$i1 = 0.5;
$i2 = 0.4;
echo ($i1 + $i2 = $x); // Outputs 1.9
Why is this? I've tried searching for this kind of variable setup without results. Is the variable $i2 being ignored? Why use this over echo ($x + $i1);? It outputs the same result.
The point is that it does two things in one statement.
It is shorthand for:
$i2 = $x;
echo ($i1 + $i2);
The assignment happens inline, saving the separate line. Not ideal style, but often used in if(), while() and other control statements.
that would be $i1 + the assignment.
The assignment evaluates to $x ($i2 = $x )
the end result is echo 0.5 + 1.4.
Even php has operator priorities http://php.net/manual/en/language.operators.precedence.php.
= is treated before +, which means that this is what happens:
echo ($i1 + ($i2 = $x));

How do you remember the order of the expressions in a for loop?

It's a pretty simple question, I always have to go check here and then I hit my head and say it's so obvious. But really after a week of not using it I usually end up writing
for ($i = 1; $i++; $i <= 10;) {
echo $i;
}
some Mnemonic might help
ICE:
Initialisation
Check
Execute
Think logical! The order is the same as the expressions are evaluated.
for ($i = 0; $i < 10; ++$i) {
echo $i;
}
// is same as
$i = 0; // 1.
while ($i < 10) { //2.
echo $i;
++$i; // 3.
}
They go in order.
for (expr1; expr2; expr3)
expr1: Evaluated once at the beginning of the loop
expr2: Evaluated at the beginning of each iteration of the loop
expr3: Evaluated at the end of each iteration of the loop
You want to initialize first, check the condition second, and increment (or decrement) your counter last.
START -> CHECK FOR DANGER -> MOVE AHEAD
for( $i = 0 ; $i < 100 ; $i++ )
Hope it helps :-)
Best of luck!
F
irst (initialisation)
O
Only while (condition)
R
Rolling on (incrementing or decrementing)
I may be daft but don't you want this structure:
for ( $i = 1; $i <= 10; $i++ )
{
echo $i;
}
I don't know of a Mnemonic to remember this structure I've always just seen it as:
STARTING OFF; DO WHILE THIS; PERFORM AFTER EACH ROTATION
Rather:
DEFINE PRIOR TO EXECUTION; DEFINE EXECUTION LIMITS; DEFINE OPERATION FOR EACH ROTATION
Just remember that the guard is always checked before the increment, so you write it before.
If you don't remember the guard is checked before the increment, you're in bigger trouble, because you don't know what the loop will do :p
SAM
Start your engine
Are we there yet?
Move

What's the difference between ++$i and $i++ in PHP?

What's the difference between ++$i and $i++ in PHP?
++$i is pre-increment whilst $i++ post-increment.
pre-increment: increment variable i first and then de-reference.
post-increment: de-reference and then increment i
"Take advantage of the fact that PHP
allows you to post-increment ($i++)
and pre-increment (++$i). The meaning
is the same as long as you are not
writing anything like $j = $i++,
however pre-incrementing is almost 10%
faster, which means that you should
switch from post- to pre-incrementing
when you have the opportunity,
especially in tight loops and
especially if you're pedantic about
micro-optimisations!"
- TuxRadar
For further clarification, post-incrementation in PHP has been documented as storing a temporary variable which attributes to this 10% overhead vs. pre-incrementation.
++$i increments $i, but evaluates to the value of $i+1
$i++ increments $i, but evaluates to the old value of $i.
Here's an example:
$i = 10;
$a = $i++;
// Now $a is 10, and $i is 11
$i = 10;
$a = ++$i;
// Now $a is 11, and $i is 11
There is sometimes a slight preformance cost for using $i++. See, when you do something like
$a = $i++;
You're really doing this:
$temporary_variable = $i;
$i=$i+1;
$a=$temporary_variable;
++$i is pre-incrementation
$i is incremented
the new value is returned
$i++ is post-incrementation
the value of $i copied to an internal temporary variable
$i is incremented
the internal copy of the old value of $i is returned
++$i //first increment $i then run line
$i++ //first run line then increment $i
this example elplains simply
<?php
$x = 10;
echo $x++. ' '.$x; // the result is 10 and 11
echo '<br>';
$y = 10;
echo ++$y. ' ' .$y; // the result is 11 and 11
// so the $x++ is not showing +1 at first but the next time
// and the ++y is showing +1 first time but not increasing next
in this case there is no difference:
for($i = 0;$i<3;++$i)var_dump $i;
/*
int(0)
int(1)
int(2)
*/
for($i = 0;$i<3;$i++)var_dump $i;
/*
int(0)
int(1)
int(2)
*/
but:
for($i = 0;$i<3; $j = ++$i )var_dump($j);
/*
NULL
int(1)
int(2)
*/
for($i = 0;$i<3; $j = $i++ )var_dump($j);
/*
NULL
int(0)
int(1)
*/
Difference is: ++$i will increment $i variable and return updated value, while $i++ will return original value, so increment it.
$prefix = 1;
$postfix = 1;
echo ++$prefix; // 2
echo $postfix++; // 1
To explain jldupont's point:
$i = 1;
$x = $i++;
echo $x; // prints 1
$x = ++$i;
echo $x; // prints 3
Another way of looking at pre and post incrementing is that it's shorthand for combining 2 statements.
Pre-incrementing
// long form
$y = $y + 1;
$x = $y; // any statement using $y
// shorthand
$x = ++$y; // the same statement using $y
Post-incrementing
// long form
$x = $y; // any statement using $y
$y = $y + 1;
// shorthand
$x = $y++; // the same statement using $y
$i++ is known as post-increment. It increments the value of $i only after assigning the original value of $i to $j first.
++$i is known as pre-increment. It increments the value of $i before assigning the value to $j, so the updated value of $i will be assigned to $j.
Hence,
$i = 4;
$j = $i++;
// Now, $i = 5 and $j = 4
$i = 4;
$j = ++$i;
// Now, $i = 5 and $j = 5
These theories apply in a similar manner for decrementing as well.
Hope this helps!
It's probably best-illustrated by an example...
Post-increment:
$zero = 0;
$n = $zero++; //$n is zero
Pre-increment:
$zero = 0;
$n = ++$zero; //$n is one
Short answer:
Prefix increases the value and returns the value increased
Postfix increases the value and returns the value before it was increased
Prefix is faster
Long answer: If you think a little about it, how you would implement those yourself, you will probably realize why prefix is faster. Truth to be told, postfix is actually (often) implemented using prefix:
const T T::operator ++ (int) // postfix
{
T orig(*this);
++(*this); // call prefix operator
return (orig);
}
Avoid postfix unless you have a specific reason not to. The difference in speed can be quite a lot for complex datatypes.
I actually looked this up a few days ago. Heres my source.
The main purpose of the post-fix increment operator is usage like this:
while(*condition*)
$array[$i++] = $something;
This is a very elegant way, how to get around some array iterations.
Breakdown:
Variable $something will be assigned to the array element indexed with $i
Variable $i will be incremented
Iteration is at the end, condition will be checked
In all other cases, you should use the prefix operator. It makes the code much more clear (You can be sure, that you already work with the incremented value of particular variable).
I ran the following code to test if ++$i is 10% faster than $i++. I admit, the code does not have a stable outcome but even then I should at least have seen some numbers near the 10%. The highest I got was 4-4.5% approximately.
<?php
$randomFloat = rand(0, 10) / 10;
$before1 = microtime(true);
for($i=0; $i <1000000; ++$i){
$rand = (rand(0, 10) / 10) * (rand(0, 10) / 10);
}
$after1 = microtime(true);
echo 'it took '.($after1-$before1) . ' seconds fot ++$i<br />';
$before2 = microtime(true);
for($i=0; $i <1000000; $i++){
$rand = (rand(0, 10) / 10) * (rand(0, 10) / 10);
}
$after2 = microtime(true);
echo 'it took '.($after2-$before2) . ' seconds fot $i++<br /><br />';
echo '++$i is '.((($after1-$before1)*100)/($after2-$before2)-100).'% faster than $i++';
Both operators still do what their syntax implies: to increment. Regardless of prefix or postfix, the variable is sure to be incremented by 1. The difference between the two lies in their return values.
1. The prefix increment returns the value of a variable after it has been incremented.
2. On the other hand, the more commonly used postfix increment returns the value of a variable before it has been incremented.
// Prefix increment
let prefix = 1;
console.log(++prefix); // 2
console.log(prefix); // 2
// Postfix increment
let postfix = 1;
console.log(postfix++); // 1
console.log(postfix); // 2
To remember this rule, I think about the syntax of the two. When one types in the prefix increment, one says ++x. The position of the ++ is important here. Saying ++x means to increment (++) first then return the value of x, thus we have ++x. The postfix increment works conversely. Saying x++ means to return the value of x first then increment (++) it after, thus x++.

Categories