I have some code that returns error Undefined variable: dblink
:
This code is from a legacy system. "dblink" variable appears only inside this function in this one file.
function db_connect($dbhost, $dbuser, $dbpass, $dbname = "") {
if (!strlen($dbuser) || !strlen($dbpass) || !strlen($dbhost))
return null;
@$$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
if ($$dblink && $dbname)
@mysql_select_db($dbname);
//set desired encoding just in case mysql charset is not UTF-8
if ($$dblink) {
@mysql_query('SET NAMES "UTF8"');
@mysql_query('SET COLLATION_CONNECTION=utf8_general_ci');
}
return $$dblink;
}
I am not entirely sure what to make of @$$variable
and $$variable
. I assume that @
means "suppress warnings", and "$$variable" just means "${value of $variable}", which does not make sense.
I am planning to rename this to just $dblink
and see what happens. There may possibly be a historical reason for this in earlier PHP versions, or maybe at one time such code did make sense, and the answer to how it made sense is what my question is seeking.