Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

John

John Avatar

*
New Member

8


February 2010
This is just a quick little tip to show you how to log any errors in PHP to a txt file. This allows you to view any unexpected errors found in your code, even if a user does not tell you of the error.


To start off we're going to make 2 new files, one called index.php, the other called log.txt.

In my index.php file I'm going to turn error reporting on, we do this like so...


<?php

error_reporting(E_ALL);

?>


This will show all errors including notices, warnings and errors.

Next we set some options using ini_set. These will override whatever options you have set in your php.ini file. And allow us to only log errors on page we want to use.


<?php

error_reporting(E_ALL);

//Make sure errors are displayed
ini_set('display_errors', 1);

//Make sure errors are logged
ini_set('log_errors', 1);

//define where our log is
ini_set('error_log', 'log.txt');

?>


So now you will see if we throw an error such as not putting a semi-colon at the end of an echo statement and using an undefined variable....


<?php

error_reporting(E_ALL);

//Make sure errors are displayed
ini_set('display_errors', 1);

//Make sure errors are logged
ini_set('log_errors', 1);

//define where our log is
ini_set('error_log', 'log.txt');

echo $foo
?>


If we run this we get the error

"Notice: Undefined variable: foo in C:\xampp\htdocs\log\index.php on line 15"

This will also appear in our log file as...

"[19-Feb-2010 18:01:37] PHP Notice: Undefined variable: foo in C:\xampp\htdocs\log\index.php on line 15"

That is all, thanks for reading! :)


Eric

Eric Avatar



1,442


November 2005
A great tool for debug and improved error messages is xdebug. Works great. You can work it into an IDE for breakpoints which are oh-so-handy.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Eric Avatar
A great tool for debug and improved error messages is xdebug. Works great. You can work it into an IDE for breakpoints which are oh-so-handy.


For reference:
xdebug.org/

Looks interesting, but I'm fine debugging normally. :P

Eric

Eric Avatar



1,442


November 2005
Chris Avatar
Eric Avatar
A great tool for debug and improved error messages is xdebug. Works great. You can work it into an IDE for breakpoints which are oh-so-handy.


For reference:
xdebug.org/

Looks interesting, but I'm fine debugging normally. :P
Normally I am too, but it helps out every once in a while to be able to run through code step-by-step.

newBookmarkLockedFalling