Tuesday, March 17, 2009

Track error messages on php

It is common in php that you write codes but whenever you tried to display on the web browser it displays nothing even a html. You want to know the behind where actually problem occurs. In which line or in which part of the code. The error_reporting function will help in this case really well.

Below is an example which is written to display directory contents inside c:\Documents and Settings folder but at the end on the browser it displayed nothing. You by enabling error reporting I might wish the say what actually happening there.

Code

<html>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
$fileHandler = opendir('C:\Documents and Settings');
while (false !== ($file = readdir($filehandler))) {
$files[] = $file;
}
sort($files);
print_r($files);
closedir($filehandler);
?>
</body>
</html>


Output

Notice: Undefined variable: filehandler in C:\apache2triad\htdocs\array_file.php
on line 8

Warning: readdir(): supplied argument is not a valid Directory resource in
C:\apache2triad\htdocs\array_file.php on line 8

Warning: sort() expects parameter 1 to be array, null given in
C:\apache2triad\htdocs\array_file.php on line 11

Notice: Undefined variable: filehandler in C:\apache2triad\htdocs\array_file.php
on line 13

Warning: closedir(): supplied argument is not a valid Directory resource in
C:\apache2triad\htdocs\array_file.php on line 13


Based on the output you are aware that there is problem with variable naming. As variable is case-sensitive in php so you can write the code as,


<html>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
$fileHandler = opendir('C:\Documents and Settings');
while (false !== ($file = readdir($fileHandler))) {
$files[] = $file;
}
sort($files);
print_r($files);
closedir($fileHandler);
?>
</body>
</html>

Related Documents
Working with cookie in php(set, retrieve, delete)
Accessing data from html forms in php
Track error messages on php
Array declaration, assignment and printing in php
Variables in PHP
Magic constants in php

No comments:

Post a Comment