1

I am trying to create a function that adds data to a PHP session array. I want to dynamically create the Key of the array item as the variable name, and set the value of said key to that variables value. My example code so far:

$foo = 'bar';
addHookpointVar($foo);

function addHookpointVar($var) {
    $_SESSION['HOOKPOINT']['foo'] = $var;
}

Is there a method in PHP to get a string representation of a variable, such that my $_SESSION['HOOKPOINT']['VAR'] will be set to the name of the variable passed in?

All the methods I have found involve looping through every variable in the $_GLOBALS

3
  • you just have to pass the name as 2nd argument. Commented Oct 22, 2015 at 20:16
  • @AziBaloch Yes, I knew that was an option but thank you for pointing that out. This question was to specifically see if there are methods to use a variables name as a string. I'm trying to write a function to do a lot of generic-manipulations where I am not required to hard-code every variable name into the function call Commented Oct 22, 2015 at 20:18
  • @ArbraCadaver is posted exactly what you want. Commented Oct 22, 2015 at 20:23

2 Answers 2

4

Here's one way:

$foo = 'bar';
addHookpointVar(compact('foo'));

function addHookpointVar($var) {
    list($key, $val) = each($var);
    $_SESSION['HOOKPOINT'][$key] = $val;
}

Or maybe cleaner for the function:

function addHookpointVar($var) {
    $_SESSION['HOOKPOINT'] = array_merge($_SESSION['HOOKPOINT'], $var);
}

However, I would probably just use an array $hp['foo'] = 'bar'; and then you can pass $hp into the function.

Sign up to request clarification or add additional context in comments.

3 Comments

This is a very nice / clean solution. I was hoping to keep it generic enough that I would be able to use a variable name as a string (so I can dynamically call the function on loops and such), but this solution is still very helpful. From the looks of it, the variable name as string is likely not possible. I'm going to leave the question open for a little while, but I'll accept this answer if nothing along those other lines comes in :) +1
I thought he did grab the var name as string though? :/ compact('foo') created an associative array where it's then represented by $var in the scope of the function and $key would be the var name (foo in this case) would it not ?
Kind of. It still requires putting in a string representation instead of directly passing a variable into it (as I mentioned, would be useful when inside of loops, etc.), which is mainly what I was hoping to accomplish. It definitely seems like this is the closest to a solution that can be accomplished, though so I'm accepting the answer
-1

I was also looking for a solution to convert a variable name as string. Having not found the correct answer, I created a function for this problem.

<?php
function func(&$variable) {
    $backupValue = $variable;
    $variable = uniqid();
    foreach ($GLOBALS as $key => $value) {
        if (!is_array($key) && $value == $variable) {
            $variable = $backupValue;
            return $key;
        }
    }
    throw new Exception('error number xx');
}

$test1 = "This is a test";
$test2 = 'This is a second test';
$test3 = 'This is a second test';

$array = [];

$array[func($test1)] = $test1;
$array[func($test2)] = $test2;
$array[func($test3)] = $test3;

var_dump($array);
?>

(Also available on Github.)

2 Comments

While the link may answer the question, links change, so Stack Overflow prefers to not depend on them. It would be better to include the essential parts of the answer here and provide the link for reference.
I didn't know, sorry! Edited!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.