php syntax $a = $b = 1; - php

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.

Related

Php variable references

Why in this code is $a equal to 21? I am giving to $b the value of $a by reference, why is $a changing as well??
$a = '1';
$b = &$a;
$b = "2$b";
echo $a.", ".$b;
Notice that you define the variables with single or double quotes (both are ok)
That's how Php knows you mean a string
Regarding the reference - the meaning of reference, is that $b points to $a, so its not really a variable
and lastly, this: $b = "2$b"; is basically string concatanation
Here's a simple explanation
$a = '1';
$b = &$a; // Sets $b to a reference to $a
echo $b."<br>"; // $b value is still one
$b = "2$b"; // here u write "2 and $b = 1" which means b = 21 and also Sets $a to 21
echo $a.", ".$b;
So your output is 21,21 hope you understand

How can a referenced value stay in memory when the original variable that holds it has a new referenced value?

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

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 reference - The confusions

I find that the reference in the code below is confusing,
$a = 4;
$b = &$a;
var_dump($b);
$a = 10;
var_dump($b); // 10
$b = 100;
var_dump($a); // 100 but shouldn't it be 10?
Value of $b is a reference to $a and $a is never a reference to $b.
But why when I change the value of $b. The value of $a changes as well?
In the line: $b = &$a; The variable $b is being set up as a reference to $a (as in it will point to the same memory location as $b). In this respect $b essentially becomes an alias or another way of accessing and modifying $a.
This link explains pointers in C++ (it's important to stress that this is not C++ but the link explains pointers / references well).
I hope this helps.
$b = &$a; represents their values are equal if you define $b then $a value becomes equal to that of $b respectively.
Check out this link for more information

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