- This topic is empty.
-
AuthorPosts
-
March 27, 2017 at 2:31 am #10165xakiruParticipant
The best/easy/fast solution that you can use if it’s a quick debugging, is to surround your code with catching exceptions. That’s what I’m doing when I want to check something fast in production.
try { // Page code } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; }
May 9, 2017 at 3:28 am #10166joel-wemboParticipantThis code on top should work:
error_reporting(E_ALL);
However, try to edit the code on the phone in the file:
error_reporting =on
May 20, 2017 at 12:07 pm #10174peterParticipantIn order to display a parse error, instead of setting
display_errors
in php.ini you can use a trick: use include.Here are three pieces of code:
File: tst1.php
<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); // Missing " and ; echo "Testing
When running this file directly, it will show nothing, given
display_errors
is set to 0 in php.ini.Now, try this:
File: tst2.php
<?php error_reporting(E_ALL); ini_set('display_errors', 'On'); include ("tst3.php");
File: tst3.php
<?php // Missing " and ; echo "Testing
Now run tst2.php which sets the error reporting, and then include tst3. You will see:
Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in tst3.php on line 4
June 4, 2017 at 2:41 am #10167lintabParticipantYou can add your own custom error handler, which can provide extra debug information. Furthermore, you can set it up to send you the information via email.
function ERR_HANDLER($errno, $errstr, $errfile, $errline){ $msg = "<b>Something bad happened.</b> [$errno] $errstr <br><br> <b>File:</b> $errfile <br> <b>Line:</b> $errline <br> <pre>".json_encode(debug_backtrace(), JSON_PRETTY_PRINT)."</pre> <br>"; echo $msg; return false; } function EXC_HANDLER($exception){ ERR_HANDLER(0, $exception->getMessage(), $exception->getFile(), $exception->getLine()); } function shutDownFunction() { $error = error_get_last(); if ($error["type"] == 1) { ERR_HANDLER($error["type"], $error["message"], $error["file"], $error["line"]); } } set_error_handler ("ERR_HANDLER", E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); register_shutdown_function("shutdownFunction"); set_exception_handler("EXC_HANDLER");
September 26, 2017 at 12:32 pm #10175sumit-guptaParticipantSet this in your index.php file:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
October 19, 2017 at 5:45 am #10163peter-haberkornParticipantIf you have Xdebug installed you can override every setting by setting:
xdebug.force_display_errors = 1; xdebug.force_error_reporting = -1;
force_display_errors
Type: int, Default value: 0, Introduced in Xdebug >= 2.3 If this
setting is set to 1 then errors will always be displayed, no matter
what the setting of PHP’s display_errors is.force_error_reporting
Type: int, Default value: 0, Introduced in Xdebug >= 2.3
This setting is a bitmask, like error_reporting. This bitmask will be logically ORed with the bitmask represented by error_reporting to dermine which errors should be displayed. This setting can only be made in php.ini and allows you to force certain errors from being shown no matter what an application does with ini_set().May 24, 2018 at 8:48 am #10164pardeepParticipant<?php // Turn off error reporting error_reporting(0); // Report runtime errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Report all errors error_reporting(E_ALL); // Same as error_reporting(E_ALL); ini_set("error_reporting", E_ALL); // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE); ?>
While your site is live, the
php.ini
file should have display_errors disabled for security reasons. However, for the development environment, display_errors can be enabled for troubleshooting.October 24, 2019 at 11:11 am #10160gvlasovParticipantIf it is on the command line, you can run
php
with-ddisplay_errors=1
to override the setting inphp.ini
:php -ddisplay_errors=1 script.php
November 26, 2019 at 2:28 am #10161nvrmParticipantIn Unix CLI, it’s very practical to redirect only errors to a file:
./script 2> errors.log
From your script, either use
var_dump()
or equivalent as usual (both STDOUT and STDERR will receive the output), but to write only in the log file:fwrite(STDERR, "Debug infos\n"); // Write in errors.log^
Then from another shell, for live changes:
tail -f errors.log
or simply
watch cat errors.log
December 31, 2019 at 10:07 am #10159shaikh-nadeemParticipantReport all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
Display all PHP errors
error_reporting(E_ALL); or ini_set('error_reporting', E_ALL);
Turn off all error reporting
error_reporting(0);
January 8, 2022 at 10:17 am #10168webcoder.co.ukParticipantAccepted asnwer including extra options. In PHP files for in my DEVELOPMENT apache vhost (.htaccess if you can ensure it doesn’t get into production):
ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL);
However, this doesn’t make PHP to show parse errors – the only way to show those errors is to modify your php.ini with this line:
display_errors = on
(if you don’t have access to
php.ini
, then putting this line in.htaccess
might work too):// I've added some extra options that set E_ALL as per https://www.php.net/manual/en/errorfunc.configuration.php. php_flag log_errors on php_flag display_errors on php_flag display_startup_errors on php_value error_reporting 2147483647 php_value error_log /var/www/mywebsite.ext/logs/php.error.log
March 13, 2022 at 5:21 am #10157really-nice-codeParticipantIf you are on a SharedHosting plan (like on hostgator)… simply adding
php_flag display_errors 1
into a .htaccess file and uploading it to the remote folder may not yield the actual warnings/errors that were generated on the server.
What you will also need to do is edit the php.ini
This is how you do it via cPanel (tested on hostgator shared hosting
plan)After logging into your cPanel, search for MultiPHP INI Editor.
It is usually found under the SOFTWARE section in your cPanel list of items.On the MultiPHP INI Editor page …you can stay on the basic mode tab and just check the button on the line that says display_errors.
Then click the Apply button to save.IMPORTANT: Just remember to turn it back off when you are done debugging; because this is not recommended for public servers.
December 3, 2022 at 2:40 am #10158theking2ParticipantAs it is not clear what OS you are on these are my 2 Windows cents.
If you are using XAMPP you need to manually create thelogs
folder underC:\xampp\php
. Not your fault, ApacheFriends ommitted this.To read and follow this file do.
Get-Content c:\xampp\php\logs\php_error_log -Wait
To do this in VSCode create a task in
.vscode\tasks.json
{   // See https://go.microsoft.com/fwlink/?LinkId=733558   // for the documentation about the tasks.json format   "version": "2.0.0",   "tasks": [     {       "label": "Monitor php errors",       "type": "shell",       "command": "Get-Content -Wait c:\\xampp\\php\\logs\\php_error_log",       "runOptions": {         "runOn": "folderOpen"       }     }   ]
and have it run on folder load.
-
AuthorPosts
- You must be logged in to reply to this topic.