Do not receive certain PHP error messages - php

I am running Apache/PHP locally, and do receive errors in many cases, like uninitialized variables - but I have noticed a few cases where I do not receive error messages and am wondering if this is normal OR if I am missing out on anything,
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
$1a = "Hello world!";
echo $1a;
?>
Also producing no error:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
function a()
{
$b = 5;
echo $b;
}}
a();
?>
Does this script product PHP errors as it should for you? If so, I am wondering why I receive none. This is not an error I am likely to make, but there have been other times where I receive a blank page like above, where I would like some sort of feedback.

Your calls to ini_set never get executed, because the error happens in the parsing phase (i.e. before anything gets executed). You need to set those configuration values earlier in .htaccess, httpd.conf or php.ini.

Open up php.ini, and CTRL-F for display_errors - is it set to on? When I installed php I used ini_set(); to try and see errors and it didn't work, but editing the php.ini file worked.
Edit: I looked at your phpinfo() output on the page you posted, and it's set to off. Turn it on if you can. If I recall correctly, you have to restart apache after you edit it.
Edit2: In your php.ini file, error_reporting should be set to E_ALL & ~E_NOTICE (default), E_ALL | E_STRICT (development), or E_ALL & ~E_DEPRECATED (production). Honestly, that's all gibberish to me - but if it helps, mine is set to E_ALL & ~E_DEPRECATED, and I recieved errors from your code.

Related

PHP 7. I'm not able to display or log errors below. Is this possible? [duplicate]

I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs

PHP cannot debug, errors only on server log [duplicate]

I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs

Turn off display errors using file "php.ini"

I am trying to turn off all errors on my website. I have followed different tutorials on how to do this, but I keep getting read and open error messages. Is there something I am missing?
I have tried the following in my php.ini file:
;Error display
display_startup_errors = Off
display_errors = Off
html_errors = Off
docref_root = 0
docref_ext = 0
For some reason when I do a fileopen() call for a file which does not exist, I still get the error displayed. This is not safe for a live website, for obvious reasons.
I always use something like this in a configuration file:
// Toggle this to change the setting
define('DEBUG', true);
// You want all errors to be triggered
error_reporting(E_ALL);
if(DEBUG == true)
{
// You're developing, so you want all errors to be shown
display_errors(true);
// Logging is usually overkill during development
log_errors(false);
}
else
{
// You don't want to display errors on a production environment
display_errors(false);
// You definitely want to log any occurring
log_errors(true);
}
This allows easy toggling between debug settings. You can improve this further by checking on which server the code is running (development, test, acceptance, and production) and change your settings accordingly.
Note that no errors will be logged if error_reporting is set to 0, as cleverly remarked by Korri.
You should consider not displaying your error messages instead!
Set ini_set('display_errors', 'Off'); in your PHP code (or directly into your ini file if possible), and leave error_reporting on E_ALL or whatever kind of messages you would like to find in your logs.
This way you can handle errors later, while your users still don't see them.
Full example:
define('DEBUG', true);
error_reporting(E_ALL);
if (DEBUG)
{
ini_set('display_errors', 'On');
}
else
{
ini_set('display_errors', 'Off');
}
Or simply (same effect):
define('DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', DEBUG ? 'On' : 'Off');
In php.ini, comment out:
error_reporting = E_ALL & ~E_NOTICE
error_reporting = E_ALL & ~E_NOTICE | E_STRICT
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ER… _ERROR
error_reporting = E_ALL & ~E_NOTICE
By placing a ; ahead of it (i.e., like ;error_reporting = E_ALL & ~E_NOTICE)
For disabling in a single file, place error_reporting(0); after opening a php tag.
In file php.ini you should try this for all errors:
error_reporting = off
Let me quickly summarize this for reference:
error_reporting() adapts the currently active setting for the default error handler.
Editing the error reporting ini options also changes the defaults.
Here it's imperative to edit the correct php.ini version - it's typically /etc/php5/fpm/php.ini on modern servers, /etc/php5/mod_php/php.ini alternatively; while the CLI version has a distinct one.
Alternatively you can use depending on SAPI:
mod_php: .htaccess with php_flag options
FastCGI: commonly a local php.ini
And with PHP above 5.3 also a .user.ini
Restarting the webserver as usual.
If your code is unwieldy and somehow resets these options elsewhere at runtime, then an alternative and quick way is to define a custom error handler that just slurps all notices/warnings/errors up:
set_error_handler(function(){});
Again, this is not advisable, just an alternative.
In file php.ini you should try this for all errors:
display_errors = On
Location file is:
Ubuntu 16.04:/etc/php/7.0/apache2
CentOS 7:/etc/php.ini
You can also use PHP's error_reporting();
// Disable it all for current call
error_reporting(0);
If you want to ignore errors from one function only, you can prepend a # symbol.
#any_function(); // Errors are ignored
Turn if off:
You can use error_reporting(); or put an # in front of your fileopen().
I usually use PHP's built in error handlers that can handle every possible error outside of syntax and still render a nice 'Down for maintenance' page otherwise:
Format PHP error on production server
Open your php.ini file (If you are using Linux - sudo vim /etc/php5/apache2/php.ini)
Add this lines into that file
error_reporting = E_ALL & ~E_WARNING
(If you need to disabled any other errors -> error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE & ~E_WARNING)
display_errors = On
And finally you need to restart your APACHE server.
It is not enough in case of PHP fpm. It was one more configuration file which can enable display_error. You should find www.conf. In my case it is in directory /etc/php/7.1/fpm/pool.d/
You should find php_flag[display_errors] = on and disable it, php_flag[display_errors] = off. This should solve the issue.
It's been quite some time and iam sure OP's answer is cleared. If any new user still looking for answer and scrolled this far, then here it is.
Check your updated information in php.ini file
Using Windows explorer:
C/xampp/php/php.ini
Using XAMPP Control Panel
Click the Config button for 'Apache' (Stop | Admin | Config | Logs)
Select PHP (php.ini)
Can i create my own phpinfo()? Yes you can
Create a phpinfo.php in your root directly or anywhere you want
Place this <?php phpinfo(); ?>
Save the file.
Open the file and you should see all the details.
How to set display_errors to Off in my own file without using php.ini
You can do this using ini_set() function. Read more about ini_set() here (https://www.php.net/manual/en/function.ini-set.php)
Go to your header.php or index.php
add this code ini_set('display_errors', FALSE);
To check the output without accessing php.ini file
$displayErrors = (ini_get('display_errors') == 1) ? 'On' : 'Off';
$PHPCore = array(
'Display error' => $displayErrors
// add other details for more output
);
foreach ($PHPCore as $key => $value) {
echo "$key: $value";
}

php parse errors won't show

I'm running ubuntu 10.04 + nginx + php-fpm 5.4
If I set display_errors = On in php.ini all errors are printed. If Instead I set that to off and then use ini_set('display_errors, '1'); directly in the script they will show as well but not the parse errors, just a blank page. I tried to play with error_reporting too and E_STRICT but I couldn't find a way!
If you disable display_errors in php.ini, and later enable it in your PHP script using ini_set(), it will only be enabled after the line containing that ini_set() call has been executed.
Parse errors occur before the PHP script even starts -- when the PHP file is parsed (hence the "parse error" name).
Which means they occur before your ini_set() has even a chance of being executed -- which means that, in your case, display_errors is not enabled when the parse error occurs ; and, as a consequence, you don't get anything displayed.
here I am years after this was answered, but I found a way around this.
For the script I'm writing, I created a second script which includes the ini_set() directives, followed by an include for the script I really am working on.
To with, here is test_run.php
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
include('./my_script.php');
?>
Aside from turning on display_errors, you can also watch error logs. Typically, running Ubuntu + apache, your error log is going to be in /var/log/apache2/error_log. To watch in real time what's happening, you run something like tail -f /var/log/apache2/error_log.
This can sometimes be more straightforward than fussing with php settings.
**You must enable error displaying in the php.ini file **
I have added ( un-commented ) the following lines, and the parse errors are being displayed now.
error_reporting
Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
Development Value: E_ALL
Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
Try error_reporting(E_ALL);. Or the docs

php, can't get warnings to appear on my test server, but in log file on my isps server?

I'm trying to get the warnings to show in my php error log.
My ISP has some warnings showing and I need to be able to see them on my test server.
error_reporting = E_ALL & ~E_NOTICE
display_errors = off
display_startup_errors = Off
log_errors = On
error_log = "C:\php-errors.txt"
I've upgraded my php version too 5.2.17
I also have
register_globals = Off
Like my ISP, but I can't get any warnings to show.
ini_set('display_errors', 'on'); is a great way to change php configurations settings specific for that page only. Include it in a global header/initialization file to make it application specific. Also, as mentioned before error_reporting(E_ALL); is good for this too.
Code at the top your scripts:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Be sure to use these only for development environments only.
Set display_errors to On to display the warnings in the page (instead of in the logfile) and set error_reporting to E_ALL | E_STRICT to display all warnings and errors in your php.ini.
If you want to show all possible warnings, try error_reporting(-1);, or you can put error_reporting = -1 into the php.ini file on your test server. It works, because the internal variable is used as a bit-field, and -1 sets all the bits on, hence, showing all possible errors.
To make sure that the error_reporting is still set to what you think, the variable returns the currently set level.
$prevErrLevel = error_reporting(-1);
echo "errlevel was: $prevErrLevel before setting to all.", __FILE__,':',__LINE__;

Categories