开发者

PHP: how do you specify that you do not want a string evaluated?

I have some php code in a database like so

$x = "<?php some code here ?>";

but I want to output that whole line to the browser without php evaluating it. Right now it is evaluating it unfortunately. I thought about escaping it but that didn't work. How might a person accomplish this?

Thanks

EDIT:

<?php

echo '<? hey ?>';
echo "<dog dog>";

?>

if I run that code the dog dog tag shows up in the browser source code where as <? hey ?> does not. It seems like it w开发者_C百科ould still be evaluating it.

Edit, got the answer, thanks everyone.


Just do:

echo htmlspecialchars($x);


'Single quotes' tell PHP to interpert the string exactly as is. It will include all whitespace and characters exactly as is.

"Double Quotes" tell PHP to parse the string. This reduces whitespace, replaces variables, and parses any other magic string things.

Finally, `backticks` are used for shell commands.

If you are trying to display it in a browser exactly like that, you might want to try htmlentities($string).


Do you want it to appear like that? If so, you'll need to use &lt; and &gt; (strictly only the &lt; is necessary) to encode the string.


use '(single quotes) instead of "(double quotes)


Ih PHP double quotes evaluate expressions, single quotes do not so:

$a = 123;
$b = "value of $a"; // value of 123
$c = 'value of $a'; // value of $a

The only problem with single quotes is they don't understand characters like \n for newlines (that will be printed as \n not a newline when put in single quotes).

So is all you need:

echo '<?php some code here ?>';

?

For more information see Strings in the PHP manual.


You're a bit unclear about what gets evaluated.

If you're talking about variables, there are plenty of correct answers here.

If you're talking about the <? ?> block, something's wrong. That string should not be evaluated if within a PHP block (If you mean the opening and closing PHP statements).

Maybe you are missing the opening and closing <? ?> before and after your operation?


If you're outputting php code you might even consider using highlight_string which will perform syntax highlighting on the input

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜