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.
Related
I found this awfully old comment in the PHP docs comments, but can't get my mind around it why it outputs "hihaha" and not "eita" in the first example. $a is changed and I'd assume that "hihaha" is removed for good. If not, then why is it so that if the change is to null or assigning a copy of another variable, then the "hihaha" IS removed for good?
// 1. example
$a = "hihaha";
$b = &$a;
$c = "eita";
$a = &$c; // why doesn't this purge "hihaha" from existence?
echo $b; // shows "hihaha" WHY?
// 2. example
$a = "hihaha";
$b = &$a;
$a = null;
echo $b; // shows nothing (both are set to null)
// 3. example
$a = "hihaha";
$b = &$a;
$c = "eita";
$a = $c;
echo $b; // shows "eita"
Is this a "good way" towards the circular references problem?
Starting with $a = "hihaha";, when you do $b = &$a;, $b is not referencing $a. It is referencing the content of $a. As it says in PHP: What References Do:
$a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place.
Then after $c = "eita";, when you do $a = &$c;, $a is now referencing the content of $c ("eita").
This does not affect $b at all. $b is still referencing the original content of $a ("hihaha"). Pointing $a at something else does not change that.
In case you have a more mspaint learning style, here is a visual aid representing the first four statements of example 1:
In the second example, $a and $b are still pointing at the same content when $a is set to null, so $b is now referencing null as well. Visually:
Think of a variable as pointing to a reference - to break down Example 1...
1
$a = "hihaha";
$a points to the reference for the string hihaha, lets call it R1
2
$b =& $a;
Here we are saying, point $b to the same reference as$a (R1)
3
$c = "eita";
$c points to the reference for the string eita, lets call it R2
4
$a =& $c;
Now we say, point $a to the same reference as $c ($b still points to R1)
At this stage,
$a and $c point to R2,
$b points to R1
- should be easy to guess what happens next!
5
echo $b; // hihaha
We now know that echoing $b will output R1!
Hope that helps!
Have a read of http://php.net/manual/en/language.references.whatdo.php
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
Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.
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
Explain this interview question to me:
Q: If the variable $a is equal to 5 and variable $b is equal to character a, what’s the value of $$b?
A: 5, it’s a reference to existing variable.
That's a variable variable. PHP will look up the variable with the name stored in the string $b. So if $b == 'a' then $$b == $a.
It's a lot like pointers in C, except they use variable name strings instead of memory addresses to point to each other. And you can dereference as many times as you want:
$a = 5;
foreach (range('b', 'z') as $L) {
$$L = chr(ord($L) - 1);
}
echo $$$$$$$$$$$$$$$$$$$$$$$$$$z;
Output:
5
-95 is the answer as if u will echo $b u will get output as
"a"
and if u echo $a u will get out but as "5"
hence in this sense when u $(echo $b) which same as $(a) hence u will get it as "5-100" which is "-95"
$$b - 100
= $a - 100 // substituting $b=a
= 5 - 100
= -95
I don't know if the '?' is erroneous in the statement '$$b? - 100' but I don't think that will compile.
However:
$a = 5
$b = 'a';
$c = $$b - 100;
$c will equal -95, because $$b is a variable variable reference and given that $a = 5 it resolves to $a (5) - 100, or -95.
the answer is -95
$a - 100
The following is a good reference on PHP variables
http://php.net/manual/en/language.variables.variable.php