Remove warning error in php

Remove warning messages in PHP

Solution 3: To suppress warnings while leaving all other error reporting enabled: Solution 4: If you don’t want to show warnings as well as errors use Error Reporting — PHP Manual Question: I am running a script where a url parameter is increasing by 1. On github of codesniffer project you can find PSR2 ruleset: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR2/ruleset.xml Solution 3: Because I have a large nubmer of files and probably some excludes, it seems a good idea to define my own ruleset.

Remove warning messages in PHP

I have some PHP code. When I run it, a warning message appears.

How can I remove/suppress/ignore these warning messages?

You really should fix whatever’s causing the warning, but you can control visibility of errors with error_reporting() . To skip warning messages, you could use something like:

error_reporting(E_ERROR | E_PARSE); 

You can put an @ in front of your function call to suppress all error messages.

To suppress warnings while leaving all other error reporting enabled:

error_reporting(E_ALL ^ E_WARNING); 

If you don’t want to show warnings as well as errors use

// Turn off all error reporting error_reporting(0); 

How to Remove Warning Messages in PHP, Use the @Sign Here is another simple method of removing warning messages with PHP. All you need to do is to put @ in front of the function you intend to run. Here is an example: @theFunctionHere (); How error_reporting Works In PHP, the error_reporting function allows setting the type of error reporting that the …

Читайте также:  Setting null value in java

Ignore warning and carry on

I am running a script where a url parameter is increasing by 1. Every so often, I get the error message below and the script comes to a halt:

Warning: file_get_contents (https://example.com?id=431): failed to open stream: HTTP request failed! HTTP/1.1 429 Please retry after few minutes

$i = 1; while($file = file_get_contents('https://example.com?id='.$i)) < echo ''.$i.'
'; $i++ >

While I know I can use error_reporting(0); to stop the warnings from appearing, my question is as follows: will the script continue running after the hidden warning?

You can, of course, disable error reporting as others have suggested. However, there are two MUCH better solutions.

First, you could use set_error_handler to create a function which converts errors into exceptions (code in Example 1 here: http://php.net/manual/en/class.errorexception.php). Then, you can simply use a try and catch to check to see if an exception has occurred, and handle it appropriately. See here: http://php.net/manual/en/internals2.opcodes.catch.php

Another solution would be to use PHP’s cURL library (http://php.net/manual/en/book.curl.php). cURL will let you check if an error has occurred when you make an HTTP request, and you can respond appropriately .

To be clear, since it seems to be OP’s only concern: both of these solutions will allow the script to continue running after an error has occurred. They also have the added benefit of allowing OP to create code that makes a predetermined programmatic response to errors rather than just blindly ignoring any and all errors with unknown results (and no indication that they even happened).

Finally, a note that’s applicable to this particular situation: HTTP 429 is «too many requests,» which makes sense given that OP is placing these requests one after another with no delay. Sleeping (http://php.net/manual/en/function.sleep.php) between HTTP requests would likely eliminate the problem entirely.

This is not something you should use in prod, but try:

error_reporting(E_ERROR | E_PARSE); 

I think it is better to check if the file is reachable before trying to read it.

Источник

How to remove warning and error messages in PHP

Error warnings for PHP are helpful during development, where you can use them for debugging. Showing the same errors and warnings in a production environment could pose a security risk as it could expose sensitive and exploitable information about the system.

Warning and error reporting in PHP are configured via the display_error error_reporting directives. display_error defines if errors are displayed at all, while error_reporting allows you to specify the type or level of errors to show.

Steps to remove error and warning messages in PHP:

$ sudo vi /etc/php/7.4/apache2/php.ini
; This directive controls whether or not and where PHP will output errors, ; notices and warnings too. Error output is very useful during development, but ; it could be very dangerous in production environments. Depending on the code ; which is triggering the error, sensitive information could potentially leak ; out of your application such as database usernames and passwords or worse. ; For production environments, we recommend logging errors rather than ; sending them to STDOUT. ; Possible Values: ; Off = Do not display any errors ; stderr = Display errors to STDERR (affects only CGI/CLI binaries!) ; On or stdout = Display errors to STDOUT ; Default Value: On ; Development Value: On ; Production Value: Off ; http://php.net/display-errors display_errors = On

Set the value to On instead to further tune the types of messages to display using error_reporting directive.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Error handling and logging ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This directive informs PHP of which errors, warnings and notices you would like ; it to take action for. The recommended way of setting values for this ; directive is through the use of the error level constants and bitwise ; operators. The error level constants are below here for convenience as well as ; some common settings and their meanings. ; By default, PHP is set to take action on all errors, notices and warnings EXCEPT ; those related to E_NOTICE and E_STRICT, which together cover best practices and ; recommended coding standards in PHP. For performance reasons, this is the ; recommend error reporting setting. Your production server shouldn't be wasting ; resources complaining about best practices and coding standards. That's what ; development servers and development settings are for. ; Note: The php.ini-development file has this setting as E_ALL. This ; means it pretty much reports everything which is exactly what you want during ; development and early testing. ; ; Error Level Constants: ; E_ALL - All errors and warnings (includes E_STRICT as of PHP 5.4.0) ; E_ERROR - fatal run-time errors ; E_RECOVERABLE_ERROR - almost fatal run-time errors ; E_WARNING - run-time warnings (non-fatal errors) ; E_PARSE - compile-time parse errors ; E_NOTICE - run-time notices (these are warnings which often result ; from a bug in your code, but it's possible that it was ; intentional (e.g., using an uninitialized variable and ; relying on the fact it is automatically initialized to an ; empty string) ; E_STRICT - run-time notices, enable to have PHP suggest changes ; to your code which will ensure the best interoperability ; and forward compatibility of your code ; E_CORE_ERROR - fatal errors that occur during PHP's initial startup ; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's ; initial startup ; E_COMPILE_ERROR - fatal compile-time errors ; E_COMPILE_WARNING - compile-time warnings (non-fatal errors) ; E_USER_ERROR - user-generated error message ; E_USER_WARNING - user-generated warning message ; E_USER_NOTICE - user-generated notice message ; E_DEPRECATED - warn about code that will not work in future versions ; of PHP ; E_USER_DEPRECATED - user-generated deprecation warnings ; ; Common Values: ; E_ALL (Show all errors, warnings and notices including coding standards.) ; E_ALL & ~E_NOTICE (Show all errors, except for notices) ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.) ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors) ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; Development Value: E_ALL ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT ; http://php.net/error-reporting error_reporting = E_ALL
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
$ sudo systemctl restart apache2

Источник

How to remove warnings in PHP.

In this guide, we will show you how to remove warning messages in PHP.

We will also explain why it is generally a bad idea to hide all warning messages.

Hiding all warning messages is basically the same as turning up the music in your car so that you can’t hear the worrying noise that your engine is making.

Hiding PHP warnings with the error_reporting function.

The error_reporting function allows us to tell PHP which errors to report.

For example, if we want to display all error messages except warnings, we can use the following line of code:

//Report all errors except warnings. error_reporting(E_ALL ^ E_WARNING);

Typically speaking, the error_reporting function should be placed at the top of your code. This is because the function can only control errors that occur in the code below it.

Can I hide PHP notice messages as well?

If you also want to hide notice messages, then you can set the following level in the error_reporting function:

//Only report fatal errors and parse errors. error_reporting(E_ERROR | E_PARSE); //This will usually create a division by 0 //warning message. echo 1 / 0; //This will usually create an array to string //notice message. echo array();

In the code snippet above, we told PHP that it should only report fatal errors (E_ERROR) and parse errors (E_PARSE).

Afterwards, we created two lines of code:

  • We divided 1 by 0, which would typically result in a “Warning: Division by zero” message.
  • We then attempted to echo out an array. Under normal circumstances, this would cause the following notice: “Notice: Array to string conversion”

If you run the example above, you will see that the page doesn’t display any notices or warnings. Furthermore, if you check the PHP error log, you will see that they also haven’t been logged.

Stopping warning messages from being displayed.

If you simply want to stop warning messages from being displayed, but not prevent them from being logged, then you can use the following piece of code:

//Tell PHP to log errors ini_set('log_errors', 'On'); //Tell PHP to not display errors ini_set('display_errors', 'Off'); //Set error_reporting to E_ALL ini_set('error_reporting', E_ALL );

Here, we are using PHP’s ini_set function to dynamically modify the settings in our php.ini file:

  1. We set log_errors to On , which means that PHP will log warnings to our error log.
  2. We set display_errors to Off . As a result, PHP will not output errors to the screen.
  3. Finally, we set error_reporting to E_ALL.

Note: If you can access your php.ini file, then you should probably edit these values directly instead of changing them on the fly.

Using the @ character to suppress errors.

In some cases, you might not have control over certain warnings.

For example, a GET request to an external API could fail, resulting in a “failed to open stream” warning. To prevent this from occurring, we could use the @ character like so:

//API URL $url = 'http://example.com/api'; //Attempt to get contents of that URL $result = @file_get_contents($url);

As you can see, we have placed the @ (AT) character next to our function call. This means that if file_get_contents fails, it will not throw an E_WARNING message.

This works because the @ character is an error control operator that tells PHP to ignore any errors.

Note that error suppression should be used sparingly. Abusing this control operator can lead to issues that are difficult to debug.

Why should I not suppress all warning messages?

An E_WARNING message is an error that does not prevent the rest of your PHP script from executing. Although it does not halt the script, it is still an error. It means that something in your code is not working the way that it is supposed to be working.

You should always try to address the issue instead of hiding it. Otherwise, it may lead to other bugs further down the line.

If you sweep these warning messages under the rug, you will be limiting your ability to spot serious problems in your application.

For example, what if your online shop had a “division by one” error that was preventing certain users from adding multiple items to their basket? Perhaps this issue only occurs when the user applies an expired coupon. Or maybe it happens after they reduce the quantity of an item in their basket.

Either way, something bad is happening and you don’t know about it.

You cannot rely on end users to report issues. Many of them will either think that they are using the website incorrectly or they will simply forget about it and move on. Internet users are impatient. They usually won’t take the time to fill out contact forms or attach screenshots.

As a result, this lazy approach of “nuking” all warning messages has caused your online shop to lose out on sales.

Источник

Оцените статью