Home What purpose does a $$ (double dollar) serve in the context of mysql?
Reply: 1

What purpose does a $$ (double dollar) serve in the context of mysql?

Jordan
1#
Jordan Published in 2018-02-14 16:42:40Z

This question already has an answer here:

  • Reference — What does this symbol mean in PHP? 18 answers
  • What does $$ (dollar dollar or double dollar) mean in PHP? 6 answers

I am using old code for a login process that accesses a local database. This is the first web project that I would like to put online. The code that I started with had deprecated functions so I've been trying to get everything up and running and gain an understanding of the language/process.

The original developers used a variable $link and two lines later they declare a global $$link. (EDIT: Since I first posted I learned about a variable variable, but I still fully understand this use case. What could it be used for)

tep_db_connect() or die('Unable to connect to database server!');
function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link')
{
  global $$link;

  $$link = new mysqli($server, $username, $password);

  if (!$$link) {
  die("Connection Failed: " . $mysqli_connect_error());
  }

 mysqli_select_db($$link, $database);
 return $$link;

}

This is my first web project. I'm used to using a debugger to figure things out.

Carlos Rodrigues
2#
Carlos Rodrigues Reply to 2018-02-14 16:51:27Z

I believe this can be considered "meta" programming, but as has been mentioned, this is a variable variable. An example to illustrate may be:

$a = '1';
$b = 'a';
echo $$b; // result: 1, can effectively be read as ${$b} -> ${a} -> $a
echo ${$b}; // same as above but a bit more obvious
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.405732 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO