How to show PHP code in a PHP file?
Moderator: General Moderators
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
How to show PHP code in a PHP file?
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?
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
Check out the source-display script at php.net:
http://www.php.net/source.php?url=/source.php
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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;
?>highlight_file() should get the job done.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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?
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?
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
highlight_string() works as well....Thanks pickle again
But I am more interested to know what is the logic behind displaying PHP code in a PHP file. How does it go uninterpreted?
Code: Select all
<?php
$string = '
<?php
echo "Hello world";
echo "<br />"."Trying to print another new line";
?>
';
highlight_string($string);
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Not sure what you mean..
Are you wanting to know how to echo out php code?
Use single quotes.
returns
Are you wanting to know how to echo out php code?
Use single quotes.
Code: Select all
echo '<?php echo "this will not parse"; ?>';Once again, I don't know what your last post really meant..<?php echo "this will not parse"; ?>
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
Code: Select all
<?php
echo '<?php echo "this will not parse"; ?>';
?>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