Leonardo Riedel Leonardo Riedel - 3 months ago 9
HTML Question

How to echo the '<?php' in HTML without starting a php section

the Title descripe it:

How to echo the

<?php
open tag, without starting a
<?php
section.
I tried it with this:

$string = str_replace("<?php","<?php",$string);


I hope someone can help me! :)

Kind regards,
Leo

Answer

PHP is not recursively embeddable. The following

<?php

echo "<?php echo 'Hi mom!' ?>";

Will output the literal characters <, ?, p etc... The "internal" echo will NOT be executed, because it's not PHP code - it's just some characters in a string.

Note that if you're outputting this text into an HTML context (e.g. a web browser), the <?php tag WILL be mis-interpreted as an invalid/illegal HTML tag, and be skipped by the browser. In which case, you have to do

echo "&lt;?php echo 'hi mom!' ?&gt;"

Remember - you're not outputting PHP code, you're outputting text, and if it's going into a web page, that text has to be VALID html.

Comments