Page 1 of 1

How to show PHP code in a PHP file?

Posted: Mon Nov 07, 2005 3:31 pm
by raghavan20
I am wondering how to show PHP code within a PHP page. I am asking about a functionality similar to posting PHP codes in this forum. Is there any special script available to hide it as non-PHP from the php engine interpreting it? What is the logic behind it?

Posted: Mon Nov 07, 2005 3:52 pm
by foobar
You just open a file using file() or fopen(). After that, you can echo() it directly or run it through a syntax hilighter.

Check out the source-display script at php.net:

http://www.php.net/source.php?url=/source.php

Posted: Mon Nov 07, 2005 4:06 pm
by raghavan20
This does not work...it won't work if I read from a file because any way it echoes it...

Code: Select all

<?php
$string = '
<?php 
echo "Hello world"; 
echo "<br />"."Trying to print another new line";
?>
';
echo "Display PHP code<br />".$string;
?>

Posted: Mon Nov 07, 2005 4:08 pm
by pickle
highlight_file() should get the job done.

Posted: Mon Nov 07, 2005 4:15 pm
by raghavan20
Yeah...highlight_file() worked pickle.
Here is an example

But if I want to display a set of PHP lines, do I have to put that in a file to read it...Is it not possible to assign PHP code in a string and used some function to display it...then how do this forum read PHP code from the database and display it...is any other function available to do th same?

Posted: Mon Nov 07, 2005 4:25 pm
by pickle
If you want to highlight a particular string, use highlight_string(). Just read the lines you want to highlight into a string, and call that function.

Posted: Mon Nov 07, 2005 6:16 pm
by raghavan20
highlight_string() works as well....Thanks pickle again

Code: Select all

<?php

$string = '
<?php 
echo "Hello world"; 
echo "<br />"."Trying to print another new line";
?>
';

highlight_string($string);
?>
But I am more interested to know what is the logic behind displaying PHP code in a PHP file. How does it go uninterpreted?

Posted: Mon Nov 07, 2005 6:25 pm
by John Cartwright
Not sure what you mean..
Are you wanting to know how to echo out php code?
Use single quotes.

Code: Select all

echo '<?php echo "this will not parse"; ?>';
returns
<?php echo "this will not parse"; ?>
Once again, I don't know what your last post really meant..

Posted: Mon Nov 07, 2005 6:32 pm
by raghavan20

Code: Select all

<?php
echo '<?php echo "this will not parse"; ?>';
?>
I can not see the output on the php file instead it shows up only in view source.
File at action
While viewing source of that file,,,scroll down to bottom...because you would see a lot of advertisements code flooding the page :)