Jump to content

PHP 8.1 How can I stop warning messages from displaying?


SLSCoder

Recommended Posts

I'm running PHP 8.1 in Apache2 on a Debian server.

I need PHP to display error messages but not warning messages.
I've got 2 config files, cli & fpm. I'm not sure which one Apache2 is using.

Both have error_reporting set like this:
error_reporting = E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT

Moreover, every time a page loads session code loads with it.
At the top of my page: SessionHandler.php is this:
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);
I've also tried changing it to this: error_reporting(E_ALL & ^ E_WARNING & ^ E_NOTICE & ^ E_DEPRECATED & ^ E_STRICT);
Still the warning messages display.

I've got a habit of trying to display values in associative arrays where the key doesn't exist.
PHP throws a warning up for doing this:
    Warning: Undefined array key "ScpFeatureBundleID"

How can I get rid of this warning without stopping errors from displaying?

 

Link to comment
Share on other sites

I know but it's just a pain in pretty near every interface.
I store db fields in the same array as page functionality fields - sometimes.
A given field (usually a functionality field) may or may not be in the array. If not, I just want an empty string.
The solve is if(isset($arMine["NoField"])) ...

For the field that threw the error I needed to debug I did that so it's fixed.
In general ... not really. I'd rather lose the warnings.

Is there no way to actually stop warnings from displaying?

 

 

Link to comment
Share on other sites

2 hours ago, SLSCoder said:

The solve is if(isset($arMine["NoField"])) ...

In the ye olden times, yes.  In the modern times, the solution to your undefined index issue is much simpler, the Null Coalescing Operator:

$arMine["NoField"] ?? '';

It might be a bit tedious to go through the code, but it's not a difficult change to make.  Take the file and line number from any warnings you see, go to that location and implement the null coalescing operator.  Repeat until you have no more warnings.

 

Link to comment
Share on other sites

  • 2 weeks later...

I fear PHP has lost site of its objective. PHP is famous because it's EASY - less so anymore with every new version.
More code is not generally a better solution.
This looks like bloatware to me: if(isset($arMine["NoField"])) . That would be hundreds of cases many of which are html input field values.
This is a *little* better: $arMine["NoField"] ?? '' Thanks. I may even use this.

ginerjm: Thanks for your responses.
If I *want* warnings I would have no problem displaying them. If I'm in production and my users encounter a bug I've gotta fix it. So if I need warnings I just turn them on. I need to be ABLE to shut them off and always could. It doesn't work anymore. I don't know why. I've explained what I did.
'ALL php code' - as I stated above, all my files require_once a session file which handles that and a lot of other stuff for every page in the app.
If I upgrade PHP it's a BIG move. PHP demolishes my code with every single new version (in the old days upgrades were better not destructive).
That to say it happens first on a dev site and once re-debugged can be used in production.
Of course debugging may well include the need to see warnings.

webdeveloper123: Thanks for your response.
I don't want the warnings to EVER show up unless I want them.
That because I don't want to have to write code to cover every single case where I use a (maybe non-existent) field value in a data array.

 

 

 

Link to comment
Share on other sites

15 hours ago, SLSCoder said:

This looks like bloatware to me: if(isset($arMine["NoField"])) . That would be hundreds of cases many of which are html input field values.

You should be checking every single input value that comes from the client because you cannot trust anything that it sends to you! 
You have no control over the client's machine so you must regard it as completely untrusted.  Just sending particular HTML to it does not count as control - just fire up the [free, installed-as-standard] "Developer Tools" in your web browser if you don't believe me.  You must validate everything server-side and that means checking and cleansing every single input datum as it arrives.  

 

15 hours ago, SLSCoder said:

If I'm in production and my users encounter a bug I've gotta fix it. So if I need warnings I just turn them on.

Would that not turn them on for every single User? 
Cue wide-spread confusion, complaints and bug reports!  

 

16 hours ago, SLSCoder said:

If I upgrade PHP it's a BIG move. PHP demolishes my code with every single new version

Yes, upgrades can be disruptive but I'd be surprised if every upgrade causes you problems. 
Or, perhaps, your codebase is simply older - the more versions you have to "jump" across, the more likely you're going to have problems. 

Sadly, software upgrading is like a diesel-powered hamster wheel - once you climb onto it and start running, it's very very hard to stop. 
(Unless you use VB6, of course - abandoned by "Our Friends in Redmond" in 2002 but still going strong today.   😉 ). 

 

16 hours ago, SLSCoder said:

I don't want the warnings to EVER show up unless I want them.
That because I don't want to have to write code to cover every single case where I use a (maybe non-existent) field value in a data array.

To me, this sounds like you're either working with highly volatile data structures that change a lot or you have a lot of "dead" code handling data items that no longer exist. 
All the more reason to guard against missing elements properly.

 

Regards, 
   Phill W. 

 

 

Link to comment
Share on other sites

  • 2 weeks later...
On 9/20/2023 at 1:10 PM, kicken said:
$arMine["NoField"] ?? '';

In view of this very ugly thread I've started using your solution every time I read a data array field.
I actually use $arMine["NoField"]??'' with no spaces. It works well.
It's a little messy (and it's gonna take forever to fix them all) but you guys win.

Thanks Kicken

  • Like 1
Link to comment
Share on other sites

On 9/20/2023 at 8:31 AM, SLSCoder said:

I need PHP to display error messages but not warning messages.

What about hiding all errors and warnings (on the production side) generated by PHP? Then write code to handle error cases the user is more likely to experience (e.g., incorrect login, invalid record ID, etc.) and output custom / user-friendly error messages.

Side note:
If you haven't already, you may want to consider the potential security impacts of outputting the raw PHP errors.
https://www.php.net/manual/en/security.errors.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.