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 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
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
What is the difference between $a and $$a in php?
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).
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. ;)
What is the purpose of the dollar sign in JavaScript?
A '$' in a variable means nothing special to the interpreter, much like an underscore.
From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.
The dollar sign function $()
in jQuery is a library function that is frequently used, so a short name is desirable.
Related Topics
PHP With MySQL 8.0+ Error: the Server Requested Authentication Method Unknown to the Client
What's the Difference Between Isset() and Array_Key_Exists()
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions
Correctly Determine If Date String Is a Valid Date in That Format
Replacing Accented Characters PHP
How to Solve "Fatal Error: Class 'MySQLi' Not Found"
How to Pass Data to View in Laravel
Why Does MySQLi Num_Rows Always Return 0
Why Is the Hash Part of the Url Not Available on the Server Side
Remove All Special Characters from a String
PHP - How to Create a Newline Character
Are PHP Variables Passed by Value or by Reference
PHP/MySQL Insert Row Then Get 'Id'
How to Echo or Print an Array in PHP