What does $$ (dollar dollar or double dollar) mean in PHP?
A syntax such as $$variable
is called Variable Variable.
For example, if you consider this portion of code:
$real_variable = 'test';
$name = 'real_variable';
echo $$name;
You will get the following output:
test
Here:
$real_variable
contains'test'
$name
contains the name of your variable:'real_variable'
$$name
mean "the variable thas has its name contained in$name
"- Which is
$real_variable
- And has the value
'test'
- Which is
EDIT after @Jhonny's comment:
Doing a $$$
?
Well, the best way to know is to try ;-)
So, let's try this portion of code:
$real_variable = 'test';
$name = 'real_variable';
$name_of_name = 'name';
echo $name_of_name . '<br />';
echo $$name_of_name . '<br />';
echo $$$name_of_name . '<br />';
And here's the output I get:
name
real_variable
test
So, I would say that, yes, you can do $$$
;-)
Double dollar sign php
The problem with $$ in PHP is that you create unknown variable names, that may override variable names you already use. It is a source for subtle programming errors, and should generally not be used.
PHP - Difference in creating variable variables with brackets or double dollar signs?
Both versions are basically the same.
The curly braces notation is used in cases where there could be ambiguity:
$foo = 'bar';
$bar = [];
${$foo}[] = 'foobar';
var_dump($bar); // foobar
If you'd omit the braces in the above example, you'd get a fatal error, with the braces, it works fine.
However, I would recommend avoiding dynamic variable names altogether. They are funny to use and may save some space. But in the end, your code will become less readable and you will have trouble debugging.
Double dollar variables with arrays
You were very close. You need to tell PHP to evaluate the $a
alone. Try:
<?php
$a = 'test';
$test = ['a', 'b'];
echo ${$a}[0];
Example
How can I mimic PHP $$ (double dollar sign) in jQuery
If you can get the name of the relevant part of your object from the class of the a
, then you can reference it like this: object["stringKey"]
or object[variable]
. Given your example:
// this object mimics the object passed to your plugin
var myobj = {
myobja: {
zero: 'obj00 zero',
one: 'obj01 one',
two: 'obj02 two'
},
myobjb: {
zero: 'obj10 aaa',
one: 'obj11 bbb'
}
}
var objectName = "myobja"; // This would be the value from the A elements' class
alert(myobj[objectName].zero)
Example fiddle
PHP dollar sign in strings
$str
contains a string with the content of "$var"
(no variable replacement, just these very characters). It was created using single quotes, so no variable replacement there.
When echoing it using echo "$str"
, the variable $str
gets replaced with its content, namely the string "$var"
, thus resulting in your output.
The string replacement in double quotes strings does not work recursively! So in order to have $str
replaced by 1024
in the second appearance, you have to create $str
using double quotes in the first place.
Accessing object variable via double dollar sign
$this->data->$this->iterator
is ambiguous. Do you mean the property of $this->data
that has the name of $this->iterator
or the iterator
property of $this->data->$this
?
You need to make it unambiguous:
$this->data->{$this->iterator}
The same goes for $$this->iterator
.
JavaScript Double Dollar Sign
I wrote this function. :)
There is no longer any special significance to the $$ prefixes. It was a notation that I used back when packer was popular. Packer would shorten variable names with this prefix. Nowadays Packer will automatically shorten variable names so this notation is redundant.
Short answer, the $ sign is a valid identifier in JavaScript so you are just looking at a bunch of ugly variable names. ;)
Double dollar variable not being called correctly in a function
Warning Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.
source
Related Topics
PHP Warning: PHP Startup: Unable to Load Dynamic Library 'Pdo_Mysql.So'
Parse Error: Syntax Error, Unexpected '.', Expecting ',' or ';'
JavaScript Cookies VS PHP Cookies
How to Use MySQL Found_Rows() in PHP
PHP Remove Commas from Numeric Strings
How to Ensure I Caught All Errors from MySQLi::Multi_Query
PHP MySQL - When Is the Best Time to Disconnect from the Database
PHP Date - Get Name of the Months in Local Language
Using PHP Code in Smarty Tpl File
Can't Connect to Postgresql with PHP Pg_Connect()
Strip HTML Tags and Its Contents
Pdoexception' with Message 'Sqlstate[22001]: String Data, Right Truncated: 0
Pull Random Line from Txt File as String
Escaping Curl @ Symbol with PHP
Warning: File_Get_Contents: Failed to Open Stream: Redirection Limit Reached, Aborting
Get/Set Dpi with PHP Gd/Imagick