How do i override the print_r() function?
I don't want to create another function, i just want to change print_r's content to echo a '<pre>' in front of it's output.
Thanx.
Override PHP builtin functions
Moderator: General Moderators
Ummm can't you just do this:
Code: Select all
function my_print_r($var){
echo "<pre>";
print_r($var);
echo "</pre>";
}I've actually written a custom function to do just this, plus a little more:
This function outputs what file & line the function was called from - useful if you've got multiple calls in a file & you've forgotten where you put them.
If you don't want to have to worry about including a long absolute or tricky relative filename in each file you want to use this, you have two options (that I know of):
Code: Select all
function dumpArray($p_array)
{
$backtrace = debug_backtrace();
$file = $backtrace[0]['file'];
$line = $backtrace[0]['line'];
echo '<pre>';
echo "File: $file<br />";
echo "Line: $line<br />";
print_r($p_array);
echo '</pre>';
}If you don't want to have to worry about including a long absolute or tricky relative filename in each file you want to use this, you have two options (that I know of):
- You can put this function in a file then put that file in your include path. phpinfo() should tell you where that is. What this'll do is allow you to just call include('customfunctions.php') from anywhere, and PHP will find the file you want.
- You can put this function in a file and then make that file automatically included at the head of every PHP document. Perhaps a little overkill, but if this is just a private server, it shouldn't be too bad. You can set the file to automatically include in php.ini.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
thanks for the answers. I was just wondering if PHP is capable of doing those things, as i think any desktop language(at least C and Delphi) can.
I really wanted to override print_r in a framework, because everyone knows that function, so if i create another one, there's one extra function to learn.
Anyways, a real kicker there pickle
It's really nice and simple. Thanx.
I really wanted to override print_r in a framework, because everyone knows that function, so if i create another one, there's one extra function to learn.
Anyways, a real kicker there pickle
Feel free. The licensing is extremely affordable at only $15 per home computer, or $250 per computer for the enterprise editionEverah wrote:That is a kickin' function pickle. I think I'll be absconding with that shortly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.