benphp Posted May 8, 2008 Share Posted May 8, 2008 I want to print PHP code to a page without running it in a PHP file. I know there's got to be some function that does this. Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/ Share on other sites More sharing options...
DarkWater Posted May 8, 2008 Share Posted May 8, 2008 No. There's not. It has to have a .php extension. Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536337 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 I want to print PHP code to a page without running it in a PHP file. I know there's got to be some function that does this. here is one way: <HTML> <BODY> <?php $string=<<<stringend <?php for (\$i=0;\$i<10;\$i++) { echo "hello world<BR>"; } ?> stringend; $string = nl2br(str_replace(" "," ",htmlspecialchars($string))); echo $string; ?> </BODY> </HTML> output in browser: <?php for ($i=0;$i<10;$i++) { echo "hello world<BR>"; } ?> there must be a way without having to escape vars.... Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536339 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2008 Share Posted May 8, 2008 If php code is enclosed in single-quotes, variables won't be parsed. You also need to use htmlentities() with the second parameter set to ENT_QUOTES so that special HTML characters (<, >, ', ") that have meaning in the browser are not operated on by the browser. You might also want to look at the highlight_string() or highlight_file() functions to get php to do this for you (with some color highlighting included.) Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536352 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 yes, good ones. actually ENT_QUOTES is not required and you may need ENT_NOQUOTES depending on the situation. i tried all 3 with the same results in my example. <HTML> <BODY> <?php $string='<?php for ($i=0;$i<10;$i++) { echo "hello world<BR>"; } ?> '; $string = nl2br(str_replace(" "," ",htmlspecialchars($string, ENT_QUOTES))); echo "<DIV>".$string."</DIV>"; ?> </BODY> </HTML> i have no experience with those functions. i will have to look into them. Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536355 Share on other sites More sharing options...
benphp Posted May 9, 2008 Author Share Posted May 9, 2008 Okay - I have to share this one. I'm using THIS forum to format my PHP. I preview the code, then select the preview table and view selected source (Firefox lets you do that). The result is a beautifully formatted, colored PHP script. When you view source, look for <td class="post" id="preview_body" width="100%"> Copy that entire line, and you have your formatted HTML. Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536410 Share on other sites More sharing options...
maxudaskin Posted May 9, 2008 Share Posted May 9, 2008 Yes there is, it is called doing this: <?php echo "<?php echo \"Hello World!\" ?>"; ?> Link to comment https://forums.phpfreaks.com/topic/104769-function-to-print-php-code-within-code/#findComment-536421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.