How to show PHP code in a PHP file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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?

Post 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?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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 :)
Post Reply