This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does $$ mean in PHP?
I am new to PHP and I don't know what the difference between $a and $$a is.
$a represents a variable
$$a represents a variable with the content of $a
example:
$test = "hello world";
$a = "test";
echo $$a;
output will be hello world
If $a = 'b' then $$a is $b.
This is a variable variable. They are evil. Use arrays instead (which do the same thing, but more maintainably and with the ability to use array functions on them).
$variable is a normal variable
$$variable takes the value of a variable and treats that as the name of a variable
eg:
$var = 'welcome';
echo $var //prints welcome
$$var = 'to stackoverflow';
echo "$var ${$var}"; //prints welcome to stackoverflow
echo "$var $welcome"; //prints welcome to stackoverflow
Double dollar is a powerful way to programmatically create variables and assign values them.
E.g:
<?php
$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen
?>
In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.
So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.
$a is the contents of the variable a, $$a is the contents of the variable named in $a.
Don't use this syntax in your own code.
$$a is a variable which name is in $a
Assuming $a = "foo";, $$a will be same as $foo
In PHP each variable starts with an $.
So for example you have the variable $a = 'var';
So $$a == $var
This new variable will have the "content" of the other variable as name.
Related
Say I had the variable $foo which has a value of "bar" and I wanted to make a variable variable from it, but also append the string "123" to it, so that the variable name is $bar123. How would I do this?
I already know that $$foo = "abc123" would make a variable $bar with the value of "abc123", but I don't know how to append a string to this variable name.
Using variable variables, you can do something as the following:
<?php
$a = "foo";
$number = 123;
$b = $a . "$number";
$$b = "Hello World!";
echo ${$b};
However, as #smith said, it is better to use associative arrays here.
I realised the solution was fairly simple:
$foo = "bar";
$x = $foo . "123"
$$x = "random important variable value"
Is there any way to make an variable not redeclarable in PHP?
I mean if I set $a = 2; and then $a = 3 the value of variable $a should still be "2".
You can use define for declaring constant :
define('MY_VALUE','2');
echo MY_VALUE;//will always gives output 2
OR
echo constant("MY_VALUE");//give same output as above
is it possible to update variable variable ?
$a = "Mr.John";
$b = "Dear $a, how are you doing?"; // $b = "Dear Mr.John, how are you doing?"
but if I update $a to something else $b won't change.
$a = "Mr.Gates"; //$b = "Dear Mr.John how are you doing?";
How can i update $b?
$b is not a variable variable. It is a string that was created by interpolating a variable in string literal; there is no way to update it dynamically based on another variable changing.
You should look at making $b a function (which returns a string) instead of a plain string, and then calling it when you need to use the string.
It's evaluated during assignment.
You can make function to deal with that.
function getMeString($a) {
return "Dear $a, how are you doing?";
}
PHP is not capable of time travel. Once you "embed" a variable inside a double-quoted string, that $whatever variable is GONE and only its value remains. PHP does not keep track of what it did to build the string, so if you change your "source" variable later on, your strings will not magically update themselves.
What about:
$b = "Dear ".$a.", how are you doing?";
That is $b is created by concatenating "Some Text" + $b + "Some other text"
UPD Sorry, in the first redaction of this comment I mixed the things up.
$a = "Mr.John";
function b() { return "Dear ".$a.", how are you doing?"; }
The variable variable is the different thing:
$a = 'john';
$$a = 'silver'; // var var
echo $john; // silver
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;
I've written and played around with alot of PHP function and variables where the original author has written the original code and I've had to continue on developing the product ie. Joomla Components/Modules/Plugins and I've always come up with this question:
How does the '&' symbol attached to a function or a variable affect the outcome?
For instance:
$variable1 =& $variable2;
OR
function &usethisfunction() {
}
OR
function usethisfunction(&thisvariable) {
{
I've tried searching through the PHP manual and other related sources but cannot find anything that specifically addresses my question.
These are known as references.
Here is an example of some "regular" PHP code:
function alterMe($var) {
$var = 'hello';
}
$test = 'hi';
alterMe($test);
print $test; // prints hi
$a = 'hi';
$b = $a;
$a = 'hello';
print $b; // prints hi
And this is what you can achieve using references:
function alterMe(&$var) {
$var = 'hello';
}
$test = 'hi';
alterMe($test);
print $test; // prints hello
$a = 'hi';
$b &= $a;
$a = 'hello';
print $b; // prints hello
The nitty gritty details are in the documentation. Essentially, however:
References in PHP are a means to access the same variable content by different names. They are not like C pointers; instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The closest analogy is with Unix filenames and files - variable names are directory entries, while variable content is the file itself. References can be likened to hardlinking in Unix filesystem.
<?php
$a = "hello"; # $a points to a slot in memory that stores "hello"
$b = $a; # $b holds what $a holds
$a = "world";
echo $b; # prints "hello"
Now if we add &
$a = "hello"; # $a points to a slot in memory that stores "hello"
$b = &$a; # $b points to the same address in memory as $a
$a = "world";
# prints "world" because it points to the same address in memory as $a.
# Basically it's 2 different variables pointing to the same address in memory
echo $b;
?>
It is a reference. It allows 2 variable names to point to the same content.