Jump to content

Trying to pass ${$myvar} in one function to another. Help


Recommended Posts

I am trying to create some variety able variable names in one function, but declaring them global is not carrying them over inside another function.

Here is an over simplified example:

myfunction($name) {

global ${$name};

${$name} = "Hello world.";


myfunction2($name2) {

echo ${$name2};


$var = "testname";


//shouldn't there now be a global variable named $testname;


//shouldn't this echo "Hello world."


Please help.

Link to comment
Share on other sites

Ok, so how can I have access to the named variables in one function to another?


I have an input array or db, that feeds the first function.  Then I have to run a separate function after all the variables have been created.  Because the second array is feed the original first input but also a properties input that has to check the values of the newly created variables.  Then were applicable run certain operations based on what some of those variables values are.


Please help.



Link to comment
Share on other sites

It looks like you might be coming to PHP from javascript.  

There are a few things to keep in mind:

  • PHP has "page scope".  Since PHP is running on the server, a PHP script is loaded, executes and ends as soon as execution has completed.
  • Variables declared in a script are not available inside functions unless you declare them using the "global" keyword inside the function.
    • Exceptions to this rule are "superglobals" created by PHP like $_SESSION, $_GET, $_POST, $_COOKIE and in fact there is a $GLOBALS variable.
      • Using $GLOBALS is a way around having to use the "global" keyword
  • Functions, classes definitions etc.  are available to be used globally once declared.  To be clear these are not variables, however.  If you instantiate an object, it is not available inside a function.

This is not really a PHP best practice, but a generally acknowledged best practice in all programming languages.  Functions should use parameters to access variables, and return a result.   This avoids the common bug/pitfall of "side effects" that can easily occur when code manipulates global variables.  In an ideal world, you have unit tests for your functions, and in order for those to be of value, you want isolation of data going in and coming out of a function.  

If a function doesn't return a result, you can now return "void", so with the available syntax, you can always declare a function return type in your function definitions.  I understand the appeal of writing functions that echo output, but with the availability of php template libraries like twig, or just the use of "PHP templating" you can avoid functions that send output, even if these examples use that. 


Here's way to do what you are asking to do, even though it is ill advised:

$foo = 'Foo set';

function myfunction($name) {
  $GLOBALS[$name] = "Hello world.";

function myFunction2($name) {
    echo $GLOBALS[$name];


// outputs Hello world.


With that said,  here's sane rewrite of your examples:

function myfunction(): string {
 return "Hello world.";

function myfunction2(string $name): void {
   echo $name;

$var = "testname";

$var = myfunction();



I realize this is not really what you were looking for, and yes PHP has "variable variable" syntax, that people avoid using.  One of the reasons for this is that you have other far better options for organizing data (arrays/objects).  PHP arrays offer features that many other languages only implement via separate constructs (ie. javascript array, map, set).

PHP arrays can be declared and/or accessed either via their 0- based element order, or by array key.  So you can have code like this:

function printLocation(string $key, array $location): void {
    $element = $location[$key] ?? 'unknown';
    echo "The value of $key is $element\n";

$location = array();
$location['city'] = 'Albany';
$location['state'] = 'NY';
$location['longitude'] = -73.756233;
$location['latitude'] = 42.652580;

$display = ['city', 'state'];

foreach ($display as $value) {
   printLocation($value, $location);

// Returns
// The value of city is Albany
// The value of state is NY


Hopefully this illustrates why PHP arrays have great utility as intermediary data structures, and also why the "variable variable" features of PHP aren't used, and should be avoided.

Link to comment
Share on other sites

Thanks, it all makes sense now. 


I cannot use arrays/objects because the function is actually taking in many variables and creating new generated variable names and also newly generated variables.  I am not a programmer at all, I am a 3D animator that has hacked my way through programming learning what is needed on the fly from the past 35 years.

I solved all my current issues with your help.  But am now stuck on a new one I know you can solve in a second.  LEt me post a new topic so others can find the help in the future.  Then I will reference it here.




Edited by 3dron
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.