Page 1 of 1
handling PHP error
Posted: Mon Oct 23, 2006 9:15 pm
by joboy
I am still on my second month of studying and programming with PHP. Realizing that locating bugs or errors is very time consuming, I would to ask anyone who can share their technique in "Handling PHP Error" so i could display customized messages rather that just sometimes looking at blank browser window.
Posted: Mon Oct 23, 2006 9:28 pm
by akimm
Have you tried?
Code: Select all
<?php
error_reporting(E_ALL ^ E_NOTICE);
?>
This will report all errors.
Posted: Tue Oct 24, 2006 2:51 am
by joboy
yup i had tried that reserve function as well as ini_set("display_error",1);
do i need to change setting in php.ini specifically turning display_error to On? by the way the PC i am using is a client..meaning i need to reboot the server everytime there's changes in the php.in file?
Posted: Tue Oct 24, 2006 3:17 am
by s.dot
yes, you will need to edit the php.ini file to turn this on. if there's a parse error, turning the ini_set("display... will still show a blank page
either change it in the ini (preferred) or set the change in a .htaccess file
Posted: Tue Oct 24, 2006 3:26 am
by CoderGoblin
Another useful tip is to use an editor which highlights obvious bugs (missing brackets etc). Personally I use eclipse (on linux) but I know everyone has their own preference.
Posted: Tue Oct 24, 2006 4:27 am
by joboy
i really delved into php.ini and it's worthy! what i noticed that the value of display_errors was indeed 'off'. after changing it to "on" and reboot the server, i was glad that errors are now reported by experimenting on removing a ";" semicolon on a single php statement as expected there was parse error! but on another page a notice was diplayed. whats the meaning of "undefined index.."?
Posted: Tue Oct 24, 2006 6:27 am
by akimm
at times when you make calls to files like
Code: Select all
<?php
$content = "hello";
#if you make this on your local machine, not on the server where text.txt exists, PHP will say undefined index, ##cuz it cannot find text.txt
$fp = fopen("text.txt", 'a');
fwrite($fp, $content);
fclose($fp);
?>
Posted: Tue Oct 24, 2006 9:19 am
by twigletmac
joboy wrote:whats the meaning of "undefined index.."?
You are referencing part of an array that doesn't exist, e.g. you'd get that error for
Code: Select all
$array = array('test' => 1, 'test2' => 2);
echo $array['test3'];
Mac
Posted: Tue Oct 24, 2006 10:38 am
by RobertGonzalez
joboy wrote:i really delved into php.ini and it's worthy! what i noticed that the value of display_errors was indeed 'off'. after changing it to "on" and reboot the server, i was glad that errors are now reported by experimenting on removing a ";" semicolon on a single php statement as expected there was parse error! but on another page a notice was diplayed. whats the meaning of "undefined index.."?
DO NOT LEAVE DISPLAY ERRORS ON FOR PRODUCTION ENVIRONMENTS! Hope that didn't sound too loud, but for security, develop with display_errors On but turn it off for production. Also, if you set your error_reporting level to E_ALL you will see all kinds of information about your code (very useful when developing, but again, you might want to turn it down a notch or two for production).
Posted: Tue Oct 24, 2006 12:04 pm
by choppsta
One of the most annoying errors is when it's the client experiencing them and all they can say is "my site's broken" and no matter how hard you try, you can't recreate the problem.
There are a couple of ini settings that can help out:
log_errors [bool]
error_log [filename]
These will log any PHP errors to a file of your choice which can be very handy in a production environment.