Confusion over the value of a variable after running a PHP program - php

I'm studying for my finals and I came across this question:
consider this following PHP code, write the output after executing it
<?php
$a=3;
$b=$a++;
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
When I output it in my text editor I get a < 3, but I don't understand why though?
I thought a is assigned to 3 and also b is assigned to a++ 3 and 3==3 so should a==3 be printed out?

No, you are using post-increment operator on $a. So, $b will be assigned a value of 3, and later, when the statement is executed, $a will increment itself by one, and become 4. So, you'll now be comparing $a as 4 and $b as 3.
Hence you get the result a > 3

The $a++ incrementation happens after the expression gets evaluated, while ++$a would happen before.
So in your case, $b was first set to 3, and then $a was increased.

$a++ tells the variable $a explicitely to increase, no matter if you assigning to another variable or not!
This gives the possibility to do things like if ($a++ > 10) { // ... in loops.
For your case you would have to take $b = $a + 1;

<?php
$a=3;
$b=$a++;
// $b = 3 and $a = 4 now
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>

I tested your code and I get:
a>3
which makes sense
$a is 3 but is increased to 4 when you do $a++
$b is just $a before the ++ action so it stays 3
Think of $a++ as $a = $a + 1 then it makes sense

Related

Return by reference a increased variable

I know this is simple example, and know why return 21, but dont understand why its working in this way php:
function increase(&$a) {
return $a++; // now i expect $a = 2, but return FIRST the reference (1), an increase later..?
}
$a = 1;
$b = increase($a);
echo $a.$b;
Special behavior with operators and byRef?
$a++ is post-incrementation
the old value of $a is returned and not the incremented value.
but in case of ++$a it is pre-incrementation, the value of $a is incremented and the new value is returned.
So, in this case if $a = 1 and you do $a++ it will return 1, while if you use ++$a it will return 2.
When $a is initially 1, the post-increment operator in the function tells PHP To return 1, and then increment $a to 2.... so the returned value assigned to $b is 1; and the by-reference ensures that the incremented $a is its new value in the global scope.
You're then concatenating $a (now 2 following the post-increment) with $b (1) giving 21

PHP If Statement (regarding numerical values) [duplicate]

This question already has answers here:
What's the difference between ++$i and $i++ in PHP?
(15 answers)
Closed 7 years ago.
$a = 3;
$b = $a++;
if ($a > $b) { echo “a > $b” }
else if ($a == $b) { echo “a = $b” }
else { echo “a < $b” }
When I work through this question, I get a=3, b=4 (3+1). Hence both the If and Else If conditions are false, so I go to Else and final answer is: a < 4.
However, the answer according to the mark scheme is: a > 3 meaning that the If condition is true. How could $a be larger than $b? Thanks
Take a look at the following statement:
$b = $a++;
The ++ is located after $a. This is the post-increment operator.
It first returns the current value of $a (3), and only then increments $a. In other words, $b is assigned with $a's current value, and then $a is incremented. So, $a is 4 and $b is 3, hence $a > $b.

PHP operator precedence "Undefined order of evaluation"?

http://www.php.net/manual/en/language.operators.precedence.php#example-115
<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
?>
The example from the php manual doesn't explain very well. Why isn't $a++ evaluated to 2, and then added to 1, so that it always becomes echo 1 + 2 // equals 3? I don't understand how it "may print either 2 or 3". I thought incremental ++ has "higher precedence" than addition +?
In other words, I don't understand why isn't it...
$a = 1;
1) echo $a + $a++;
2) echo 1 + ($a = 1 + 1);
3) echo 1 + (2);
4) echo 3;
It can be either 2 or 3. However in most of the time it will be 3. So why it MIGHT be 2? Because PHP is not describing in which order expressions are evaluated, since it might depends on the PHP version.
Operator precedence in PHP is a mess, and it's liable to change between versions. For that reason, it's always best to use parentheses to group your in-line equations so that there is no ambiguity in their execution.
The example I usually give when asked this question is to ask in turn what the answer to this equation would be:
$a = 2;
$b = 4;
$c = 6;
$val = $a++ + ++$b - 0 - $c - -++$a;
echo $val;
:)
Depending where I run it now, I get anything between 4 and 7, or a parser error.
This will load $a (1) into memory, then load it into memory again and increment it (1 + 1), then it will add the two together, giving you 3:
$a = 1;
$val = $a + ($a++);
This, however, is a parser error:
$a = 1;
$val = ($a + $a)++;
Anyway, long story short, your example 2) is the way that most versions will interpret it unless you add parenthesis around ($a++) as in the example above, which will make it run the same way in all PHP versions that support the incrementation operator. :)
Order of evaluation isn't a precedence issue. It has nothing to do with operators. The problem also happens with function calls.
By the way, $a++ returns the old value of $a. In your example, $a++ evaluates to 1, not 2.
In the following example, PHP does not define which subexpression is evaluated first: $a or $a++.
$a = 1;
f($a, $a++); //either f(1,1) or f(2,1)
Precedence is about where you put in parentheses. Order of evaluation can't be changed by parentheses. To fix order of evaluation problems, you need to break the code up into multiple lines.
$a = 1;
$a0 = $a;
$a1 = $a++;
f($a0, $a1); //only f(1,1)
Order of evaluation only matters when your subexpressions can have side-effects on each other: the value of one subexpression can change if another subexpression is evaluated first.

Why 1 + decrementing the value + 1 = 2?

I found a piece of code (from one of our developer) and I was wondering why the output of this is 2?
<?php
$a = 1;
$a = $a-- +1;
echo $a;
thanks
I'll give my explanation a whirl. We're talking about a variable referencing some value off in the system.
So when you define $a = 1, you are pointing the variable $a to a value 1 that's off in memory somewhere.
With the second line, you are doing $a = $a-- + 1 so you are creating a new value and setting that to $a. The $a-- retrieves the value of the original $a, which is 1 and adds 1 to make 2 and creates that value somewhere else in memory. So now you have a variable $a which points to 2 and some other value 1 off in memory which along the way decremented to 0, but nothing is pointing at it anymore, so who cares.
Then you echo $a which points to your value of 2.
Edit: Testing Page
$a-- decrements the value after the line executes. To get an answer of 1, you would change it to --$a
<?php
$a = 1;
$a = --$a +1; // Decrement line
echo $a;
?>
What the?
Just to clarify the other answers, what you have going on in this line:
$a = $a-- +1;
Basically when PHP evaluates $a--, it actually returns the value of $a, and then runs the operation of decrementing it.
Try this
$a = 1;
echo $a--; //outputs 1;
echo $a; //outputs 0;
When you run this code, you will see that the number only decrements after it has been returned. So using this logic, it's a bit more clear why
echo $a-- + 1;
would output 2 instead of 1.
A better way
Perhaps a better way, arguably more clear would be
$a = $a -1 + 1
$a = 1; /* $a is 1 */
$a = ($a--) /* returns 1 and decrements the copy of $a */ + 1 /* 1 + 1 = 2 */;
echo $a; /* 2 */
The above is equivalent to something like:
$a = 1; /* $a is 1 */
$temp = $a + 1; /* 1 ($a) + 1 = 2 */
$a = $a - 1; /* decrements $a */
$a = $temp; /* assigns the result of the above operation to $a */
echo $a;
That actually pretty much what PHP translates that into, behind the scenes. So $a-- is not such a useful operation, since $a is going to be overwritten anyway. Better simply replace that with $a - 1, to make it both clearer and to eliminate the extra operation.

Operator precedence issue in Perl and PHP

PHP:
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
echo $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
echo $a.'-'.$b;
Output 6-16-2.I don't understand the 1 here.
Perl :
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
print $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
print $a.'-'.$b;
Output 6-66-2, I don't understand the second 6 here.
Anyone knows the reason?
Actually I know && has higher precedence than and,but I still has the doubt when knowing this before hand.
UPDATE
Now I understand the PHP one,what about the Perl one?
Regarding Perl:
Unlike PHP (but like Python, JavaScript, etc.) the boolean operators don't return a boolean value but the value that made the expression true (or the last value) determines the final result of the expression† (source).
$b=1 && $a=5
is evaluated as
$b = (1 && $a=5) // same as in PHP
which is the same as $b = (1 && 5) (assignment "returns" the assigned value) and assigns 5 to $b.
The bottom line is: The operator precedence is the same in Perl and PHP (at least in this case), but they differ in what value is returned by the boolean operators.
FWIW, PHP's operator precedence can be found here.
What's more interesting (at least this was new to me) is that PHP does not perform type conversion for the increment/decrement operators.
So if $b is true, then $b++ leaves the value as true, while e.g. $b += 1 assigns 2 to $b.
†: What I mean with this is that it returns the first (leftmost) value which
evaluates to false in case of &&
evaluates to true in case of ||
or the last value of the expression.
First example
$a = 2;
$b = 3;
if($b=1 && $a=5) // means $b = (1 && $a=5)
{
var_dump($b); //bool(true) because of &&
$a++;
$b++; //bool(true)++ ==true, ok
}
echo $a.'-'.$b;
hope you will not use those codes in production)
I'm noob in perl but i can suggest a&&b returns a or b (last of them if all of them converted to bool), not boolean, then $b = (1 && $a=5) returns $b=5 (is 5)
here's the issue: 1 && 5 returns 5 in perl. you get the result you expect if you code the conditional as if(($b=1) && ($a=5))
For Perl, fig. 2: and has a very low priority in perl, it's not a synonym of &&'s. Therefore the sample is executed as (($a = 5) and ($b = 1)) which sets $a and $b to 5 and 1 respectively and returns a value of the last argument (i.e. 1).
After ++'s you get 6-2.
refer to http://sillythingsthatmatter.in/PHP/operators.php for good examples

Categories