php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27938 Variable variable causing undefined variable error
Submitted: 2004-04-09 22:31 UTC Modified: 2004-04-09 22:35 UTC
From: davelowe at davelowe dot net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.5 OS: Windows 2000 Pro
Private report: No CVE-ID: None
 [2004-04-09 22:31 UTC] davelowe at davelowe dot net
Description:
------------
I have a column value coming back from a mysql query that is '%$g_userName'

Now, when I look at the column for each row and see a leading '%', I know that the value is actually the name of a variable that exists in my application.  Here's a code snippet, where $name contains the column's value as returned by mysql (the value is '%$g_userName'):

if (substr($name, 0, 1) == '%') {
   $actualName = substr($name, 1, strlen($name) - 1);
   $name = $$actualName;
}

When the code above gets executed, I get the following error:

Notice: Undefined variable: $g_userName in C:\htdocs\ebbs\scripts\rightMenu_inc.php on line 51


I added the following immediately after the if {...} to make sure $g_userName actually exists and contains a value:

echo $g_userName.'<br />';

And now I get the following:

Notice: Undefined variable: $g_userName in C:\htdocs\ebbs\scripts\rightMenu_inc.php on line 51
dave

Where 'dave' is the value I had hard-coded in $g_userName.



Reproduce code:
---------------
$g_userName = 'dave';
$name = '$'.'g_userName';
$trueName = $$name;

echo $trueName;


Expected result:
----------------
dave

Actual result:
--------------
Notice: Undefined variable: $g_userName in C:\htdocs\ebbs\scripts\pageFooter_inc.php on line 7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-09 22:35 UTC] davelowe at davelowe dot net
Darnit, you have to:

$g_userName = 'dave';
$name = 'g_userName';
$trueName = $$name;

I needed to strip off the leading '$' to get my code to work, sorry about the wasted bandwidth.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 01 02:01:31 2024 UTC