note 11748 added to Variable variables

From: Date: Mon, 05 Mar 2001 20:26:08 +0000
Subject: note 11748 added to Variable variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-11900@lists.php.net to get a copy of this message
I was attempting to use a variable that is established outside of any function, this variable is named using the value of a field in an array, and the array is already redefined within the function. <BR>Using the following command in a function yields a syntax error: <BR>global ${$myarray["myfield"]}; <BR>{}'s seem to be the problem, as removing them, leaving the more ambiguous form of the command works: <BR>global $$myarray["myfield"]; <BR>To keep the variable variable from being ambiguous, define it before using it in a global command: <BR>$variable_name = $myarray["myfield"]; global $$variable_name; <BR>This is also a problem when using variable variables to extend variable names, such as: <BR>global ${extention_$variable}; <BR>Again, the best solution is to define it first: <BR>$extended_variable .= "extention_"; $extended_variable .= $myarray["myfield"]; global $$extended_variable; <BR>I don't know whether it brings up the syntax error in all situations or setups, but if you are getting such an error, hopefully this will help. http://www.php.net/manual/en/language.variables.variable.php

« previous php.notes (#11900) next »