views:

79

answers:

7

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 would still be evaluating it.

Edit, got the answer, thanks everyone.

A: 

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

Itay Moav
That didn't seem to make any difference, it still won't output the string. echo '<? hello ">'; Doesn't output anything to the screen
John Baker
I still don't understand what you're asking here. Do a view source. Do you see <? hello > ? If so, you need to encode the less-than according to my answer.
Skilldrick
You said to put it in single quotes right? (just confirming) That is what I did. However it is not viewable in the browser view-source.
John Baker
+3  A: 

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.

Skilldrick
+3  A: 

'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).

Chacha102
Yes this achieved exactly what I was looking for. It now outputs the "<? hey ?>" perfectly so that it's now visible in the source code and is visible
John Baker
A: 

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.

cletus
A: 

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?

Pekka
+4  A: 

Just do:

echo htmlspecialchars($x);
Alix Axel
A: 

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

AndrewMurphy