PHP eval() function
<?php
$test = 'success'; //Case 1. simple variable $string = 'This is a $test'; // Case 2. Indexed Array variable $string = 'This is a $_SERVER["SERVER_NAME"]'; echo $string . "<br>"; eval("\$string = \"$string\";"); echo $string . "<br>"; ?> Case 1. Works fine. Case 2. Errors out at the eval() function. Yes, I've tried escaping the quotes, doubling them and what not!! I dont want to use string concatenation to make this work. |
It seems to work like this:
Code:
<?php explains how to do variable substitution in quoted strings: ,----[ http://www.php.net/manual/en/language.types.string.php ] | Complex (curly) syntax | | This isn't called complex because the syntax is | complex, but because you can include complex | expressions this way. | | In fact, you can include any value that is in the | namespace in strings with this syntax. You simply | write the expression the same way as you would outside | the string, and then include it in { and }. Since you | can't escape '{', this syntax will only be recognised | when the $ is immediately following the {. (Use "{\$" | or "\{$" to get a literal "{$"). Some examples to make | it clear: `---- I hope this helps. <BroadHint> If it does, please notice that I have an Affero button. </BroadHint> |
Geez, that is EXACTLY what I was looking for. Kicks butt!!
Thanks a ton, leed25. I was about to conclude that the PHP parser wont be able to handle such a construct. |
All times are GMT -5. The time now is 04:12 PM. |