Log in

View Full Version : PHP Error Log



bluewalrus
12-15-2009, 04:52 PM
Can someone explain to me how the php error log works? Is the line number the number in just the php code for example:


<?php
1. echo "hi";
2. echo "bye"
3. echo "error above"
?>



<?php
1. echo "hi";
?>
<h1>welcome to my webpage</h1>
<?php
2. echo "bye"
3. echo "error above"
?>

Would both of those say error on line 2, or would the second one say on line 3? With requires and includes would it say the error was in the required/included page, or on the page it's being brought into and extend the line numbers? Thanks.

pilotvtm
12-15-2009, 04:58 PM
read here:
http://www.w3schools.com/PHP/php_ref_error.asp

they have some examples too...

http://www.w3schools.com/PHP/func_error_log.asp

try within the example to put 1,2,3 errors...
example:


<?php
$test=2;

if ($test>1)
{
error_log("A custom error has been triggered",
1,"someone@example.com","From: webmaster@example.com");
}
?>

bluewalrus
12-15-2009, 06:24 PM
I already have the error message, I need to know how to interrupt it. If line 26 is just of php source code in the specified file, if it is line 26 of just php in all php being brought into the page, or if it is line 26 of all the code on the page?

djr33
12-15-2009, 07:18 PM
Constants __LINE__ and __FILE__ hold the values used often in errors. They are absolute references to the line number and filename, like index.php line 32, and should never do anything funny like be referencing an included page or a different "relative" line number, etc.

However, if there is a syntax error PHP's error reporting may be off by a line or two (should be no more than three), if, for example, there is a semi colon missing. Long blocks of html outside of the php tags can also confuse things sometimes. Infinite loops are the only major exception: when there is an infinite loop, the error may be at a reported for a completely random line because that is where the script actually stopped, regardless of where the actual typo is. And in lesser cases missing brackets and parentheses can cause problems with the line number being off, but this is fairly rare.

Your questions above:
1. includes should respond as separate pages, unless you are using an error log that skips writing filenames in which case you'd have very little information.
2. blocks of html like that CAN mess up the line numbers, but it happens rarely. It should be correct based on line number in the file. I believe I have had this happen most often at the start of a page and rarely once everything is going:

<?php
echo 'hi';
?>
bye
<!--that should not cause problems -->

hi
<?php
echo 'bye';
?>
<!--this, at the very start of your code, might be more likely to do so-->


In general debugging in PHP is smooth because the error reporting is good. Sometimes it gets tougher when something like this happens or when there is a long process that doesn't work (like if a variable has a strange value that then throws and error and you don't know where the value came from). Once you get into a more complex situation like that, the only solution I have found is to strategically /*comment*/ out chunks of the page to find out where the problem is, and, if necessary, add in echos/print_r()s, etc. along the way to be sure that the expected values are true: in this case exit() is helpful. Just go step by step and exit() after that step to check. It can take a while, but it's guaranteed to eventually find the problem (a combination of those two methods, that is).

jscheuer1
12-16-2009, 02:17 AM
PHP novice or journeyman here. It occurs to me that you could easily construct a test of code to determine that (what line number is being reported). Also, it is my impression, though as I say I'm not real experienced in these matters, that the line reported is the line in the file (give or take a few lines), regardless if there is non-PHP code involved in the file. That is to say that each actual line break in the file is counted as a new line.

The reason why the line number may vary is as djr33 has said here or elsewhere. A syntax error on one line may not trigger the error until a few (or in extreme cases, many) lines later when it finally becomes clear to the PHP interpreter that no possible legal instruction can be read from the code as written.

This also happens in other programming languages. The set of instructions of any given code can have multiple possible meanings until a terminus is reached. It is only at that point that the interpreter can tell for sure whether or not what preceded it is valid or not.

Errors like these (which often comprise the vast majority of errors reported by interpretors), are more common in languages like javascript and PHP where the syntax is relatively loose. They more often than not have nothing to do with whether or not the code will perform as expected. Just whether or not the code will perform at all.

bluewalrus
12-16-2009, 04:54 AM
I've reducded the php in the file down to one include but am still getting this error message:
[15-Dec-2009 23:48:54] PHP Notice: Undefined offset: 1 in C:\Inetpub\wwwroot\file_resource\php\find.php on line 26

Full Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
<script src="http://maps.google.com/maps?file=api&v=2&key=ASKFHAFK7DlJuFhx2QQLPMOOwqbU2dfvbHPRyx4R87tPWtgr_ht1v42FIxQ8Nv1ZKoPLBF-B1MSToqBoZy2817Q" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var geocoder;
var map;
var location2;
function showLocation() {
map = new GMap2(document.getElementById("map"));
geocoder = new GClientGeocoder();
geocoder.getLocations(document.forms[0].address2.value, function (response) {
if (!response || response.Status.code != 200)
{
alert("Sorry, we were unable to geocode the second address");
}
else
{
location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
calculateDistance();
}
});
}
function calculateDistance()
{
try
{
var endlocation = new GLatLng(location2.lat, location2.lon);
marker = new GMarker(endlocation);
// Add the marker to map
map.clearOverlays();
map.addOverlay(marker);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
var bounds = new GLatLngBounds();
bounds.extend(endlocation);
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
// highlightCurrentMarker();
document.getElementById('results').innerHTML = 'Location Found. <a href="find.php?address=' + endlocation + '">Click here to search.</a>';
}
catch (error)
{
alert(error);
}
}
//]]>
</script>
<link rel="stylesheet" type="text/css" media="screen" href="find.css" />
</head>
<body onunload="GUnload()">
<?php
include('menu.php');
?>
<div class="float">
<h1>Find an Address</h1>
<form action="#" onsubmit="showLocation(); return false;">
<input type="text" name="address2" value="Find Location" class="address_input" />
<input type="submit" name="find" value="Search" />
</form>
<p id="results"></p>
</div>
<div id="map"></div>
</body>
</html>

I'm assuming this means it is in the menu.php?

djr33
12-16-2009, 05:22 AM
"offset" confuses me. I had a strange error with that once that I eventually found was actually an undefined array key, which apparently is (at least sometimes) called an "offset".

My error logs (that is, the error logs on the servers I've used) give the filename. If no filename is given, then I suppose you can assume the main file. Maybe it only gives the filename if it is a different file?

Yes, John, what you said sounds accurate. For the most part other chunks in a file don't mess with this, but occasionally, something ends up strange by a couple lines for formatting reasons. I've had PHP files that are actually wrong by 100 lines because of broken syntax. I was modifying something for a message board that had something like 700 lines and the errors were all very wrong. I eventually figured out it was something like 105 lines off since line 300 because of a missing bracket or something. I don't remember the details now, but stuff like that can do strange things. So can comments, and sometimes blocks outside of the php code. As far as I'm concerned these are errors with the parser, but that's something you have to deal with when using PHP. For the most part the errors are very accurate/helpful.


Also, you can always google the key phrases in the error message and you will find the most common strange issues related to certain error messages.

jscheuer1
12-16-2009, 06:40 AM
In a case like that, I'd write a a very simple menu.php that couldn't possibly have an error in it. Then try again, if the error goes away - it was in the menu.php file. Or just load up menu.php by itself and see what happens.

One last thing for now, the error indicates that this is being run on some sort of local 'sandbox'. These can sometimes give rise to erroneous errors that will disappear when the page is on a real server.

djr33
12-16-2009, 06:51 AM
I'm not sure if PHP is affected by running on the local machine. To do so you must setup your computer as a server so it is effectively a full server. That may still be a factor, but it's not similar to Javascript in a truly local situation.

jscheuer1
12-16-2009, 07:20 AM
It depends upon the local setup. WAMP is one where I've heard of at least some things not going as they would in 'real life'.