handling PHP error

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
User avatar
joboy
Forum Newbie
Posts: 24
Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines

handling PHP error

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

Have you tried?

Code: Select all

<?php
error_reporting(E_ALL ^ E_NOTICE);
?>
This will report all errors.
User avatar
joboy
Forum Newbie
Posts: 24
Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
joboy
Forum Newbie
Posts: 24
Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines

Post 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.."?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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);
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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.
Post Reply