Page 1 of 1

Display PHP source at bottom of page

Posted: Fri Nov 19, 2010 9:21 pm
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?

Re: Display PHP source at bottom of page

Posted: Sat Nov 20, 2010 3:35 am
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.

Re: Display PHP source at bottom of page

Posted: Sun Nov 21, 2010 3:08 am
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 />";
}



?>

Re: Display PHP source at bottom of page

Posted: Sun Nov 21, 2010 11:08 am
by Weirdan

Code: Select all

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