2

How can we debug a PHP script when display_errors is off and PHP is running in safe mode ?

6 Answers 6

8

The best way is to use error_log() http://php.net/manual/en/function.error-log.php And you can set specific handler to that method. Either record it to database, file or simply display the error.

If you want to set handler method you can use: set_error_handler

Sign up to request clarification or add additional context in comments.

Comments

2

If you're in a development environment you should disable safe_mode and enable error_reporting. On a production environment you should probably not do live debugging (since it's live). If you want to track down errors that seem to be only present on the production environment use error_log, or use the logging mechanism of the framework you are using, and check the logs afterwards. This way you won't break the normal operation of your production environment. Some frameworks like symfony provide a separate interface to access your system in another environment (for example accessing index.php has error reporting disabled but frontend_dev.php has not)

If you don't have a separate production and development environment it is high time to separate the two. You can for example install XAMPP or similar to your development PC, where you can do the development and only deploy to the live servers if you have already debugged / tested / etc. your code.

Comments

1

You can use that:

error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log','my_file.log');

PHP writes log file to file.

If you can't enable display errors directive, you can using var_dump, debug_backtrace and exceptions for debugging php source code.

Comments

0

When ever error display is off, only way we can debug the code with echo $var; exit; OR var_dump($list); exit;

Comments

0
var_dump($variable);
exit;

OR

echo $variable;
exit;

OR

echo "<pre>";
print_r($variable);
exit;

Comments

0

"Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead"

--Martin Fowler

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.