SLSCoder Posted September 20, 2023 Share Posted September 20, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/ Share on other sites More sharing options...
Barand Posted September 20, 2023 Share Posted September 20, 2023 By far the best the best way is to fix whatever they are warning you about. 3 Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1611981 Share on other sites More sharing options...
SLSCoder Posted September 20, 2023 Author Share Posted September 20, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1611988 Share on other sites More sharing options...
ginerjm Posted September 20, 2023 Share Posted September 20, 2023 Taking the easy way out..... Not a typical programmer's solution to things. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1611989 Share on other sites More sharing options...
ginerjm Posted September 20, 2023 Share Posted September 20, 2023 And what about when your system upgrades and starts flagging warnings as deprecated or 'no longer allowed' and it will no longer run without fixing these things that you don't want 'displaying'? Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1611997 Share on other sites More sharing options...
kicken Posted September 20, 2023 Share Posted September 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612000 Share on other sites More sharing options...
SLSCoder Posted September 20, 2023 Author Share Posted September 20, 2023 OK, thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612006 Share on other sites More sharing options...
webdeveloper123 Posted September 21, 2023 Share Posted September 21, 2023 Do you want the warning messages to not display because you have moved from development to live mode, or just don't want warning messages to appear while your developing? Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612011 Share on other sites More sharing options...
SLSCoder Posted October 5, 2023 Author Share Posted October 5, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612244 Share on other sites More sharing options...
ginerjm Posted October 5, 2023 Share Posted October 5, 2023 If you get an error/message because you have attempted to address a non-existent value it is a sign of simply sloppy programming. That is what needs to be fixed, not the visibility of the messages. Rough talk I know but it is what it is. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612252 Share on other sites More sharing options...
Phi11W Posted October 6, 2023 Share Posted October 6, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612261 Share on other sites More sharing options...
SLSCoder Posted October 6, 2023 Author Share Posted October 6, 2023 Thanks. I've got every one of your problems here covered. Your illusions about my code are unfounded and incorrect. This conversation is over. This forum did not help me - again. Thank you for your time. Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612273 Share on other sites More sharing options...
SLSCoder Posted October 18, 2023 Author Share Posted October 18, 2023 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612452 Share on other sites More sharing options...
ginerjm Posted October 18, 2023 Share Posted October 18, 2023 Welcome to good programing! Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612453 Share on other sites More sharing options...
cyberRobot Posted October 25, 2023 Share Posted October 25, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/317305-php-81-how-can-i-stop-warning-messages-from-displaying/#findComment-1612562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.