Can someone explain to me in a simple language why $a = 21 in the final output?
$a = '1';
echo $a . "<br>"; // result 1
$b = &$a;
echo $b . "<br>"; // result 1
$b = "2$b";
echo $b . "<br>"; // result 21
echo $a . "<br>"; // result 21 WHY?
echo $a . ", " . $b; // result 21, 21
Thank you. I appreciate the help very much.
It's because when you do
$b = "2$b";
it means "Set the value of $b to the string "2" followed by whatever the current value of $b is.
Earlier you put
$b = &$a;
This means "create a new reference for $a and call it $b", or in other words make $b point at the same thing in memory that $a is pointing at.
When you update the value of $b you're really updating the value that's stored in the memory block that both $a and $b point at, so once you've set $b to a particular value $a will be the same value because they both reference the same thing.
Related
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
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
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 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;
Answer: 100, it’s a reference to existing variable.
But I don't understand why the result is 100? who can explain this to me?
Thanks a lot!!!
Not sure what exactly you're asking, but when I run this code:
<?php
$a = 5;
$b = 'a';
echo $$b;
?>
I get output of:
5
This code gives me "5".
$a = 5;
$b = "a";
echo ($$b);
I think you may have a problem with your code/logic?
$a=5;
$b=a;
echo $$b;
Output: 5
$a=5;
$b='a';
echo $$b;
Output: 5
$a=5;
$b="a";
echo $$b;
Output: 5
$$ is a variable variable because all of the above are looking for a variable a they will all assume $a - unless you have another reference to different variable somewhere in your code which is 100.
Of course it should be 5 !!
$a = 5 ;
$b = 'a' ;
$$b = $( $b ) = $ ( 'a' ) = $a = 5 ;
If $b was ever declared to be a reference to another variable elsewhere in your code, then varaible variables won't work as expected.
<?php
$a = 5;
$b = 'a';
echo $$b, "\n"; // echoes 5 as expected
$b = &$a;
$b = 'a';
echo $$b, "\n"; // echoes 'a'