Hey, Basically, in my code below, I am echoing $code, within $code is: <img src=\"". $file . "\" /> (stored in the database) I want to be able to define $file, as I have done earlier in my code. However when I echo out $code, it shows <img src=\"". $file . "\" /> and not <img src=\"WHAT I DEFINE AS $FILE\" /> Someone told me to use eval? How would I do it?
$str = '<img src=\"". $file . "\" />'; $file = 'image.jpg'; eval('$new_str = "' . $str . '";'); echo $new_str; PHP:
No! Do not use eval() unless it is really needed. I think this was just a syntax issue. crazyryan, can you post some more of the code, as I'm sure a non-eval() solution can be found.
I tried: $display = mysql_query("SELECT * FROM `types` WHERE `type` = '" . $type . "'"); $codetype = mysql_fetch_array($display); $str = $codetype['code']; eval('$code = "' . $str . '";'); echo $code; PHP: But got: Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1 Parse error: syntax error, unexpected '"' in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1 EDIT: This was due to the row in the database, I have fixed it now. @krt: I really can't see any other way other than doing an if statement for every single file type. EDIT2: Forgot to say, huge thanks
// str_replace() because we only want to escape double quotes. eval('$code = "' . str_replace('"', '\"', $str) . '";'); PHP:
I'm having a bit of trouble with the rows in the database, for one of the codes I have: <object type=\"application/x-shockwave-flash\" data=\"" . $file . "\" width=\"" . $width ."\" height=\"" . $ height . "\"> <param name=\"movie\" value=\"" . $file . "\" /> </object> But I'm getting: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /home/phpmedia/public_html/arcade/game.php(28) : eval()'d code on line 1 I'm not using nico's code atm, when I did try the variables weren't being set.