Alex Rivera | Logout

How to display PHP code on HTML page in browser?

Asked 2011-08-05T15:54:15.457
9

How can I write PHP code in PHP? I want to do this, but it doesn't work:

<?php echo '<?php echo \'aoeu\'; ?>'; ?>

Hope someone can give me a hint,

Many thanks

Edit
Report

4 Answers

13
<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>
answered 2011-08-05T15:57:00.583
2
<?php echo '&lt;?php echo \'aoeu\'; ?&gt;'; ?>

If you don't escape the '<' and '>' they will be printed as html tags.

answered 2011-08-05T15:57:59.163
0

This cannot be done directly in the way that you posted.

PHP is a server side scripting language as I am sure you know. This means it is executed before the page is shown to the user.

In effect, your code is writing the text to the browser, but not to the server. The only way to execute php code generated by other php code would be to write the new code to a file, then run the file once the page loads.

answered 2011-08-05T16:00:02.023
0

Example 1

<?php       echo '<pre>', highlight_string('<?php
include($_SERVER["DOCUMENT_ROOT"] . "/myclass/myform.php");
?>', true), '</pre>';       ?>

Example 2
See php manual highlight-string

answered 2011-08-05T16:18:05.983

Your Answer