2

The following php code generates a PHP Parse error on the second eval() function call:

parse error, expecting "identifier (T_STRING)"' or"variable (T_VARIABLE)"' or `"number (T_NUM_STRING)"' in demo.php(19) : eval()'d code on line 1

It appears that php is unable to handle the array reference $keys['key1'] inside the code to be evaluated. I have tried both versions 5.5.30 and 7.0.3 with similar results. Is this a bug in php, or am I doing something wrong? Is there a known workaround?

Since I am sure that some users will tell me that eval is evil and that I should not use it, what I am trying to do is populate a template form with predetermined values based upon an array of keys. I could write my own php code to do this, but it would be far more complex than simply calling eval.

<?php
    $keys = array (
        'Key1' => 'Successful',
    );

    $key1 = 'Key1';

    echo 'PHP Version: ' . phpversion () . "\n";

    $evaluated = 'Failure';
    $php_works = '$evaluated = "value=$keys[$key1]";';
    echo "Evaluating the following works: " . $php_works . "\n";
    eval ($php_works); 
    echo $evaluated . "\n";

    $evaluated = 'Failure';
    $php_broken = '$evaluated = "value=$keys[' . "'key1'" . ']";';
    echo "Evaluating the following does NOT work: " . $php_broken . "\n";
    eval ($php_broken); 
    echo $evaluated . "\n";
?>
5
  • It should be $php_broken = '$evaluated = "value=$keys[\'key1\']";'; or similar Commented Feb 21, 2016 at 0:20
  • why are you evaling in the first place? Commented Feb 21, 2016 at 0:20
  • The code you provided is breaking the $php_broken string by having ' without concatenating the rest of the string after. You're muddling up the quotes. Commented Feb 21, 2016 at 0:21
  • 1
    shouldn't it be 'Key1' instead of 'key1' ? Commented Feb 21, 2016 at 0:21
  • To everyone who contributed an answer, thank you. Matt, escaping the single quotes inside of the double quotes does not appear to be required. RST, you are correct about the capitalization of Key1. Mike, I am using eval as a very easy way to populate a template html form based upon an array of values. Commented Feb 21, 2016 at 1:00

1 Answer 1

2

In php, this doesn't work:

echo "$keys['Key1']";

You have to write in one of the following ways:

echo "{$keys['Key1']}";
echo "$keys[Key1]";

So, your $php_broken string must be changed in this way:

$php_broken = '$evaluated = "value={$keys[' . "'Key1'" . ']}";';

or in this way:

$php_broken = '$evaluated = "value=$keys[Key1]";';

Edit: as noted in comments, you also have to use 'Key1' instead of 'key1'.

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

1 Comment

Thank you. I was not aware of the "{$keys['Key1']}" syntax. Also, I have corrected the capitalization of Key1.

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.