Return by reference a increased variable - 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

Related

PHP increment feature

Can someone please explain why this happens:
$a[0] = 1;
$a[0] = $a[0]++;
echo $a[0];
In this code, a[0] always becomes 1. Even if $a[0] = $a[0]++; is done multiple times it does not increment the value of a[0].
but if we assign to a different variable like this:
$a[0] = 1;
$b[0] = $a[0]++;
echo $a[0];
$a[0] will be set to 2. (And of course b[0] will be 1).
I cannot understand why this happens.
Simplify this to remove the index. It is not needed.
$a = $a++;
First, the right side is executed. Because the ++ is after the variable, it says "return $a and then increment $a." It does exactly that. It returns $a to the assignment operation and then increments $a.
After the right side is executed, the assignment operation runs. The right side returned $a before it was incremented. So, it is still the original value of $a. That is what $a is assigned to. This overwrites the increment operation that just took place on the right side.
From the PHP documentation:
PHP supports C-style pre- and post-increment and decrement operators.
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
(see http://php.net/manual/en/language.operators.increment.php)
So when using post-increment ($x++), if you assign the result to another variable, you'll end up with the value of the variable before the increment has taken place.
Okey, Lets start with first one. You assign the value of $a[0] as 1, you apply an increment operation on it, but store again in $a[0], The $a[0] is not update yet cause the increment is post-increment. But if you did it as pre-increment then you will get the value 2.
Ex: 1
$a[0] = 1;
$a[0] = $a[0]++;
echo $a[0]; //1
See the effect of pre-increment:
$a[0] = 1;
$a[0] = ++$a[0];
echo $a[0]; //2
Ex:2
Same as example one, you did the post-increment, this time you store in different variable that means, the $a[0] not updated here and the increment operation implement. so you got the result as 2. Here the post and pre both is same.
$a[0] = 1;
$b[0] = $a[0]++;
echo $a[0]; //2
Here the value of $b[0] will be same as the value of $a[0] at this stage. But if the pre-increment applied here then the value of $b[0] also changed and its stores 2.
Note: All you have to understand the pre-increment and
post-increment. For more visit -
language.operators.increment.php
Beacuse your ++ is after the variable, which means it will increment after the ++ operator has returned the original value and the assinment operator reassigns it back to the original value.
Since you are reassigning the variable before the increment, the number never has a chance to increment..
The proper way to do it is to just drop the reassignment..
$a[0] = 1;
$a[0]++;
echo $a[0];
https://3v4l.org/Tijrb
You could also move the ++ to the start and it will behave as expected:
$a[0] = 1;
$a[0] = ++$a[0];
echo $a[0];
https://3v4l.org/9fZ3U

php syntax $a = $b = 1;

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.

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

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

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.

php variable $a contains variable $b in it update $b and $a should automatically update

I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;

Categories