Display PHP source at bottom of page

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
tynen
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 9:19 pm

Display PHP source at bottom of page

Post by tynen »

I know this isn't something people would typically want for a real site. But I'm currently in the process or learning PHP. My friend is giving me "homework assignments" and then I'll show them to him at the due dates. I would really like it if the PHP code was at the bottom of my pages. Can anyone think of a way to do this?
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Display PHP source at bottom of page

Post by mikecampbell »

You can use $_SERVER['SCRIPT_FILENAME'] to get the current script, and then use one of the file system functions like file_get_contents() to read the file. Then just dump the contents to the screen, replacing line breaks with <br>s.
tynen
Forum Newbie
Posts: 5
Joined: Fri Nov 19, 2010 9:19 pm

Re: Display PHP source at bottom of page

Post by tynen »

You're the man! Thank you, my previous problem was I was using

Code: Select all

$_SERVER['PHP_SELF']
But your suggestion works perfect:

Code: Select all

<?php

$file = $_SERVER['SCRIPT_FILENAME'];
$fh = fopen($file, 'r') or die('Could not open file!');

$data = file($file) or die('Could not read file!');
foreach ($data as $line){
    echo $line."<br />";
}



?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Display PHP source at bottom of page

Post by Weirdan »

Code: Select all

//.... your php code here ....
highlight_file(__FILE__);
Post Reply