Is it possible for PHP file to print itself, for example <?php some code; ?>
that I get output in HTML as <?php some code; ?>
(I know its possible in c++), if not is it possible to actually print html version of php code with nice formatting and colors such as from this url inside code container http://woork.blogspot.com/2009/07/twitter-api-how-to-create-stream-of.html. OR from this website when you press code, while posting your example your code gets wrapped or whatever term is for that, makes it distinguishable from other non-code text. tnx
views:
144answers:
2
+1
A:
Yes.
<?php readfile(__FILE__)
__FILE__
is a magic constant that contains the absolute filesystem path to the file it is used in. And readfile
just reads and prints the contents. And if you want to have a syntax highlighted HTML output, try the highlight_file
function or highlight_string
function instead.
Gumbo
2009-09-23 14:45:27
Of course, you'd want to run it through htmlentities() and posiblely wrap it in <pre> </pre> so that it would display correctly.
Tom
2009-09-23 14:49:31
So lets say if I wanted to print content of index.php from print.php , my code would be <?php echo readfile("index.php")?>
c0mrade
2009-09-23 14:54:46
@c0mrade: Yes, that would print the plain contents (so the source) of that *index.php* directly to the client. If you want to print it in a HTML file, use `file_get_contents` instead of `readfile` and pass it through `htmlspecialchars` to replace HTML’s special characters. So: `echo htmlspecialchars(file_get_contents("index.php"));`
Gumbo
2009-09-23 15:03:24
Yes it totally worked
c0mrade
2009-09-23 15:46:10
+2
A:
I'm not sure if this is exactly what you want but you can print a file using:
echo file_get_contents(__FILE__);
or syntax-highlighted:
highlight_file(__FILE__);
Greg
2009-09-23 14:46:09