Q: When is an error not an error?

Channel Digital

on .

User Rating:  / 0
PoorBest 

A: When it's a warning

We have noticed a number of clients upgrading to new PHP5.4 servers and then finding all sorts of "errors" being reported in previously working code. The issue is not that the code has actually stopped working, but that their new server has error reporting set to report everything, including PHP warnings when the old server happily ignored the anything at Warning level or below.

In this blog I'll describe to you how to set up your error reporting properly and how to tweak your code to run happily on PHP5.4...

There are several issues here.

One is that your code shouldn't be generating warnings in the first place. However, some programming 'standards' evolve when you don't see the warning messages - for example, previously when a form submitted to a script, the first thing the script would do would be to check whether one of the form fields had been set; if so then we validate the data and do some processing, otherwise we simply present the form to the user. The old method for checking whether a $_POST variable has been sent would be to write:

if ( (int)$_POST['myvar'] != 0)
{
// code here
}

On your old server this ran fine but is now throwing an 'undefined variable' warning if $_POST['myvar'] had never been sent - for example this was the first time the script has run and we need to present the form to the user. The old server supressed the warning which was always being produced.

A tidier solution is:

if (isset($_POST['myvar'] && (int)$_POST['myvar'] != 0)
{
// code here
}

This forces php to check whether the variable exists before trying to check its value thus stopping the warning from being generated.

This can happen with any php variable, $_POST/$_REQUEST/$_GET, $_SESSION etc

If you have a function that stands a good chance of failing (for example, the $_POST, or a file open) you can suppress the warning at source by putting @ before the variable:

if (@(int)$_POST[‘myvar’] != 0)
{
// code here
}

A second cause of error is deprecated function calls. PHP is continually evolving, and some function calls that used to exist are removed in subsequent versions. Just before the function is removed, PHP is set to produce an E_DEPRECATED warning if you call the function, although the function still executes. Examples include the split function - replaced by the explode function, and the mysql library which is being replaced by the mysqli library.

Curing these warnings takes more effort as you will need to find a replacement for the deprecated function.

A third way of stopping the error messages is to change the php.ini file to suppress warning messages, like the old server used to. This usually requires access to the server files which not everyone has got.

The php.ini file recommends:

; DEFAULT VALUE : E_ALL & E_NOTICE - Show all error except for warnings and coding standard warnings
; DEVELOPMENT VALUE : E_ALL | E_STRICT - Show all error except for warnings
; PRODUCTION VALUE : E_ALL & ~E_DEPRECATED - Show everything

To make the change, edit your php.ini file (wherever it is located, each server seems to locate the file in a different directory) and search for

error_reporting =

Set the error_reporting level to your required level. Save the changes and exit the editor then restart apache to pick up the changes.

If you cannot change your php.ini file you can achieve the same result by adding the line:

error_reporting (xxx);

to all your PHP scripts, where xxx is the same levels as are used in the php.ini file.

Yet another solution is to add the error reporting line into the htaccess file for your site:

# general directive for setting php error level
php_value error_reporting integer

There are several common values used for “integer”, including:

Value

Constant

Description

1

E_ERROR (integer)

Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted.

2

E_WARNING (integer)

Run-time warnings (non-fatal errors). Execution of the script is not halted.

4

E_PARSE (integer)

Compile-time parse errors. Parse errors should only be generated by the parser.

8

E_NOTICE (integer)

Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.

16

E_CORE_ERROR (integer)

Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP.

32

E_CORE_WARNING (integer)

Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP.

64

E_COMPILE_ERROR (integer)

Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine.

128

E_COMPILE_WARNING (integer)

Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine.

256

E_USER_ERROR (integer)

User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error().

512

E_USER_WARNING (integer)

User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error().

1024

E_USER_NOTICE (integer)

User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error().

2048

E_STRICT (integer)

Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. Since PHP 5 but not included in E_ALL until PHP 5.4.0

4096

E_RECOVERABLE_ERROR (integer)

Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state.

If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR. Since PHP 5.2.0

8192

E_DEPRECATED (integer)

Run-time notices. Enable this to receive warnings about code that will not work in future versions. Since PHP 5.3.0

16384

E_USER_DEPRECATED (integer) 

User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error(). Since PHP 5.3.0

32767

E_ALL (integer)

All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0. 32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously

 To set the required level, add the relevant integer values together. To set E_ALL less a value, subtract the integer value from E_ALL's value

Obviously by setting your error reporting level to ignore warnings you run the risk of your code failing when a E_DEPRECATED function is removed in a new version of PHP. It is a good idea to get into the habit of getting your development server running with all errors being produced so that you tidy them up before moving your code over to the production server.

We use cookies to improve our website and your experience when using it. To find out more about the cookies we use and how to delete them, see our Privacy Policy.

I accept cookies from this site
EU Cookie Directive plugin by www.channeldigital.co.uk