Open In App

How to get a variable name as a string in PHP?

Last Updated : 17 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Use variable name as a string to get the variable name. There are many ways to solve this problem some of them are discussed below:

Method 1: Using $GLOBALS:

It is used to reference all variables available in the global scope. It is an associative array containing the reference of all variables currently defined in the global scope.

Example: This example uses $GLOBAL reference to get the variable name as a string.

<?php

// Declare and initialize a variable
$test = "This is a string";

// Function that returns the variable name
function getVariavleName($var) {
    foreach($GLOBALS as $varName => $value) {
        if ($value === $var) {
            return $varName;
        }
    }
    return;
}

// Function call and display the
// variable name
print getVariavleName($test);

?>

Output
test

Method 2: Using $$ Operator

The $$var_name is known as reference variable where $var_name is a normal variable. The $$var_name used to refer to the variable with the name as value of the variable $var_name.

Example:This example uses $$ operator to get the variable name as a string.

<?php

// Declare and initialize a variable
$name = "test";

// Reference variable to store string
$$name = "This is a string";

// Display result
print($name);

?>

Output
test

Using debug_backtrace()

Using debug_backtrace() in PHP, you can extract a variable name from the debug information. This involves reading the specific line of code where the function was called.

Example

<?php
function getVariableName()
{
    $trace = debug_backtrace();
    $line = file($trace[0]["file"])[$trace[0]["line"] - 1];
    preg_match('/\\$(\w+)/', $line, $matches);
    return $matches[1];
}

$myVar = "Hello, World!";
$varName = getVariableName();
echo $varName . PHP_EOL; // Output: myVar
?>

Output
varName

The get_defined_vars() function returns an array of all defined variables, both local and global. By searching this array, we can find the key (variable name) corresponding to a given value.

Example:

<?php
// Function to get the variable name as a string
function getVariableName(&$var) {
    // Get all defined variables in the current scope
    $definedVars = get_defined_vars();
    
    // Search for the variable in the defined variables array
    $variableName = array_search($var, $definedVars, true);
    
    // Return the variable name if found, otherwise return null
    return $variableName !== false ? $variableName : null;
}

// Example variables
$exampleVar = "test value";
$anotherVar = 42;

// Get the variable names
$exampleVarName = getVariableName($exampleVar);
$anotherVarName = getVariableName($anotherVar);

echo "The variable name of exampleVar is: " . $exampleVarName . "\n";
echo "The variable name of anotherVar is: " . $anotherVarName . "\n";
?>

Output
The variable name of exampleVar is: var
The variable name of anotherVar is: var

Next Article

Similar Reads

three90RightbarBannerImg