David Lorenzana
David Lorenzana

Reputation: 111

How to display PHP code on HTML page in browser?

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

Upvotes: 9

Views: 15986

Answers (14)

itoctopus
itoctopus

Reputation: 4251

In your case, you should user htmlspecialchars instead of htmlentities - htmlspecialchars is just lighter - htmlspecialchars will only convert special characters (that may cause problems in the rendering of the HTML) to their HTML entity equivalent, such as >, <, single/double quotes.

htmlspecialchars will convert all characters that have an HTML entity equivalent, such as á, é, etc...

Both will work, and the lazy (non-efficient) decision is to just always use htmlentities without considering the context of the code.

Upvotes: 0

Sanxofon
Sanxofon

Reputation: 981

As a summary of many answers here you can echo o print php and html code by changing all special characters to its correspondent html special codes. Al least all '<' chars in string should be converted to '&lt'. There are many ways of doing this:

echo htmlspecialchars("<b>\"á'");
>> &lt;b&gt;&quot;á'

This converts all characters needed for echoing code.

echo htmlentities("<b>\"á'");
>> &lt;b&gt;&quot;&aacute;'

This does the same as htmlspecialchars but also converts all characters that have corresponding html codes.

Something worth noting is that single and double quotes have a different behavior as single quotes do not interpret variables inside them nor read escaped characters as such (except the single quote itself).

$foo = "value";

echo "I replace $foo with value and \n with line break";
>> I replace value with value and 
   with line break

echo 'I don\'t replace $foo with value nor \n with line break';
>> I don't replace $foo with value nor \n with line break

Also you could use heredoc syntax, that is equal as a double-quoted string but you don't need to escape them inside the string

echo <<<EOF
Your "string' here
EOF;

Or nowdoc syntax wich is the same but imitates a single-quoted string

echo <<<'EOF'
Your "string' here
EOF;

You can read all about these here: https://www.php.net/manual/es/language.types.string.php

Upvotes: 0

BARIS KURT
BARIS KURT

Reputation: 538

You can use highlight_string or highlight_file if you use highligh_file you have to indicate php file that you want to show

<?php
highlight_string('<?php echo\'hello\' ?>');
highlight_file("D:\local\ajax_bottom.php");
?>

or you can combine both which the best solution for me:

<?php

$string=highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php");
highlight_string("$string");
?>

or you can combine in a different way:

<?php


highlight_string(highlight_file("D:\local\hatirlaticilar\Adminn\autocomplete\gethint.php"));
?>

Upvotes: 2

user3734651
user3734651

Reputation: 42

 echo '<div class="row"><div class="col-md-6 col-md-offset-3"><div class="alert alert-success" role="alert"><p style="text-align: center;">'. php code goes here .'</p></div></div></div>';

Upvotes: -1

Gerard ONeill
Gerard ONeill

Reputation: 4102

I'm unsure what the php engine needs (I'll take other poster's words for it that htmlentities works), but using vim the ?> at the end will make it look like the php code section has ended.

Instead I just do this:

string = "<? echo somefunc('strparam'); ?" . ">";

Editor is happy. If there is a better way of doing this, let me know!

Upvotes: 0

hornetbzz
hornetbzz

Reputation: 9357

Example 1

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

Example 2
See php manual highlight-string

Upvotes: 0

Aido
Aido

Reputation: 1260

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.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

If you output some complex code definitely use a template engine like smarty.. otherwise your code will look a complete mess.

I once patched the propel ORM which does output PHP code without using a template engine. It generates all the model classes based on the XML configuration files. Their code was a big mess. Don't do it.

Upvotes: 3

Phil
Phil

Reputation: 11175

I am assuming you're trying to show the <?php ?> tags? If so, just use &lt instead of <

Upvotes: 0

Pheonix
Pheonix

Reputation: 6052

Try This

<<< is Heredoc

<?php

echo <<< EOF

<?php
echo <<< EOF
EOF;
?>

EOF;

?>

Upvotes: 2

potNPan
potNPan

Reputation: 747

<?php echo '&lt;?php echo \'aoeu\'; ?&gt;'; ?>

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

Upvotes: 2

genesis
genesis

Reputation: 50976

<?php echo htmlspecialchars('<?php echo \'aoeu\'; ?>'); ?> 

http://sandbox.phpcode.eu/g/b1316.php

Upvotes: 0

Brad
Brad

Reputation: 163232

Escaping the string is all you need to do. You have done it correctly. Unless of course you are outputting for HTML, then use htmlspecialchars().

Upvotes: 0

Paweł
Paweł

Reputation: 2234

<?php echo htmlentities('<?php echo \'aoeu\'; ?>'); ?>

Upvotes: 13

Related Questions