tags:

views:

111

answers:

5

I am using PHP 4, the only way I know of to cause an error and stop everything is calling die(). But in case I run into the error later and don't remember where its coming from I would like to specify the page and line number that the die() occurred on (like other php errors do). Is there a way to do this?

Thanks!

+4  A: 

You should look into the magic constants.

echo __LINE__; // Line number

You can also run error_log() to send errors quietly to the log.

Ólafur Waage
+4  A: 

The simplest way is to use:

echo __FILE__ . ": line " . __LINE__;
die();

If you were to use PHP5, you could also use Exceptions:

throw new Exception("My error message!");

The stack trace will reveal the whole call stack and the line this was thrown on.

(EDIT: Thanks to [@John Isaacs] and [@Emil H] for informing me that Exceptions weren't added to PHP until PHP5)

Jukka Dahlbom
didn't know about exceptions, thanks!
contagious
I believe Exceptions were added in v5.
John Isaacks
Correct, Exceptions isn't available before v5.
Emil H
Thanks for the correction, I tried to do a cursory lookup to see when exceptions were added, but could not spot it.
Jukka Dahlbom
+2  A: 

In addition to @Jukka Dahlbom and @Ólafur Waage's suggestions you might also consider using debug_backtrace().

Greg
A: 

Better use error_log() to report an error and debug_backtrace() for debugging. You could also write your own error handling function (see set_error_handler()) to combine both.

Gumbo
+4  A: 

I think you should use trigger_error() to generate an E_USER_ERROR or E_USER_WARNING. This allows you to control the behaviour in detail. For example you can specify whether the messages should be shown at all using error_reporting(), or handle the E_USER_WARNING:s explicitly using set_error_handler().

Emil H
+1, didn't know of trigger_error before this.
Jukka Dahlbom