-3

I wonder how I can use the exact opposite of the eval function.

This is my code:

$test = 1;

$t = '$test';

echo opposite_eval($t);

I have to 1 output from above codes, how can i use method, function or class ?

Thanks for your estemeed help friends !

Rang
  • 3
  • 2
  • 2
    .... what? .... – ishegg Feb 23 '18 at 01:13
  • what is the expected output? Literally just `1`?? – Jeff Feb 23 '18 at 01:15
  • With eval you can run code from a variable. What do you mean with oposite? – daddykom Feb 23 '18 at 01:16
  • 1
    may be you want that: http://php.net/manual/en/language.variables.variable.php – Jeff Feb 23 '18 at 01:18
  • 2
    Using variable variables usually indicates you should be using an array. Check yourself, before your wreck your code. What you're doing is generally something to avoid. As your code grows, this technique will make your code harder to read and manage. Relevant reading: https://stackoverflow.com/questions/543792/php-variable-variables – mickmackusa Feb 23 '18 at 01:40
  • 1
    More required reading: https://stackoverflow.com/questions/3582043/variable-variables-when-useful and https://stackoverflow.com/questions/3555057/variable-variables You will notice every time someone asks a question about variable variables, those-in-the-know will advise heavily against the practice. Please heed the warnings. – mickmackusa Feb 23 '18 at 01:46
  • 1
    its looking like an X-Y issue, instead of asking about your purposed solution, delete this and ask about the problem you think this is the answer to –  Feb 23 '18 at 01:57

2 Answers2

1

i cheated a little by removing the $ from the $t string (you can do that in the function its just a string:

$t = 'test';

function opposite_eval($t){
$test = 1;
return($$t);

}

echo opposite_eval($t); //=1

the phase you want to look in to is variable variables

  • @Rang your welcome, its not used a lot, so make sure your using the right tool for the job. –  Feb 23 '18 at 01:31
  • thanks for your notice. well, how can i use this system for array item, etc: `$x = ['foo', 'bar']; $y = 'x[1]'; echo $$y;` – Rang Feb 23 '18 at 01:39
  • @Rang is there a real world case for this or are you just experimenting? –  Feb 23 '18 at 01:41
  • i use this way on our live projects, is there a problem in this situation ? – Rang Feb 23 '18 at 01:48
  • 2
    seems very 'messy', and hard to maintain, the fact you had to ask this says a lot. there will be a better approach here –  Feb 23 '18 at 01:50
-1

I think you want a variables variable

In your case it would be:

$test = 1;
$t = 'test';
echo $$t;
// output: 1

Addon:
You could also do things like that:

$test['x'] = 1;
$t = 'test';
echo $$t['x'];

Whereas this will not work:

$test['x'] = 1;
$t = "test['x']";
echo $$t;
// Produces: NOTICE Undefined variable: test['x'] on line number 6

neither will:

$test = new stdClass();
$test->x = 1;
$t = "test->x";
echo $$t;

but this will work:

$test = new stdClass();
$test->x = 1;
$t = "{$test->x}";
echo $t;

and this will also work:

$test =[];
$test['x'] = 1;
$t = "{$test['x']}";
echo $t;
Jeff
  • 6,895
  • 1
  • 15
  • 33
  • well, how can i use this system for array item, etc: $test['x'] = 1; i should use this definition as string like as your example. – Rang Feb 23 '18 at 01:31
  • thanks so much for your interest but i have exactly this structure `$x = ['foo', 'bar']; $y = 'x[1]'; echo $$y;` and i need get `1` output as use my structure. – Rang Feb 23 '18 at 01:44
  • `$y = "{$x[1]}"; echo $y;` will output 'bar' – Jeff Feb 23 '18 at 01:48
  • 2
    _but_ as @mickmackusa has stated: it looks like there is something wrong in the general design if you think you need this... – Jeff Feb 23 '18 at 01:50
  • @Rang I, too, played with variable variables a few years back... It was fun until I realized the mess I was making. Let me spare you the misery. – mickmackusa Feb 23 '18 at 01:52
  • micmackusa, what happened ? – Rang Feb 23 '18 at 02:00
  • 1
    @Rang Nothing epic happened, I just wasted my time and had to rewrite code. There is a whole suite of array functions and language constructs that make array type data a breeze to work with. Variable variables are clumsy -- full stop. When you want to iterate them or process several of them at a time, you will be writing clunky/nasty code. There is also the risk that you lose control of the namespace in your global environment and suffer unexpected collisions. The time to re-think your data storage is now. ...we can help you if you post your actual (minimized) situation (as a new question). – mickmackusa Feb 23 '18 at 02:09