0

Is it possible to check if a variable exist using a variable value? Like

//some variables:
$variable_a;
$variable_b;
$variable_c;
$variable_d;

$variable = '$variable_b';
     if (the content $variable which is '$variable_b' exists == true){
    
}

or how to make a variable value into a variable? Like

$variable = 'variable_name'; ...some code to make 'variable_name' a variable

Zoe
  • 27,060
  • 21
  • 118
  • 148
bscs 2020
  • 3
  • 2
  • Can you explain a bit more? Not sure I understand. – ficuscr Feb 23 '22 at 08:01
  • Does this answer your question? [Check if a variable is undefined in PHP](https://stackoverflow.com/questions/30191521/check-if-a-variable-is-undefined-in-php) – nice_dev Feb 23 '22 at 08:09
  • More specifically, check [this](https://stackoverflow.com/a/66253469/4964822) there. We also expect you to do some Google search before asking. – nice_dev Feb 23 '22 at 08:10
  • @nice_dev I'm not talking about isset() because I think it checks the variable itself instead of the variable value. Not really sure though – bscs 2020 Feb 23 '22 at 08:18

1 Answers1

0

You can use variable variables in PHP, and then check if such a variable has a value. For instance:

$variableA = 'Hello';
$variableB = 'variableA';
echo ${$variableB};

returns Hello on your screen. You can also check if $variableA has a value by doing:

if (isset(${$variableB})) {
    ....
}

Note that in your question you have variables that have no value, they are not set. The whole purpose of variables is to have a value, hence their name, so your variables are some kind of zombies, not really alive, not really dead. They are not set, so isset() will return false.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33