Error Suppression @ Not Working

error-suppression, php

Solution

Not a big fan of error suppression here except for throw-away scripts or instances where there really is no good way to catch an error.

Let me explain the behavior of the `Uninitialized string offset` error. It's not a bug:

Example #1

$a = 0;
$b = $a['f5'];

`$a` is a numeric scalar value. In the second line PHP is implicitly casting this numeric value to a string. The string `'0'` has a length of 1.

In PHP you can lookup a character in a string using an array index, as PHP stores strings as arrays internally. For instance:

$s= 'abcd';
print_r($s[1]);

The output of this code will be `b` as it is the second character in the string. In example #1 the lookup `'f5'` is being converted to a number as strings can only be indexed by character position. `echo intval('f5');` shows us what PHP interprets the `'f5'` string as `0` in a numeric context.

With me so far? Here's what happens when we apply this to example #2

Example #2

$a = '';
$b = $a['f5'];

`$a` is zero-length string. The second line is the same as `$b= $a[0];` - i.e., the second line is asking for the first character of a zero-length string, but the string contains no characters. So PHP throws the following error, letting you know the index simply does not exist:

Notice: Uninitialized string offset: 0 in C:\websites\tcv3\wc2009\htdocs\aatest_array.php on line 3

These are the hard knocks of programming in a loosely typed language.

Problem

I've been happily using the error suppression operator on my PHP dev setup. But recently have gotten hit with Notices like so: Notice: Uninitialized string offset: 0 in C:\websites\xxx\htdocs\includes\myscript.php on line 35 Line 35: $file_name = @$File['file_name']; I have display_errors on, and error_reporting set to 6143 (E_ALL). Am I missing something? Shouldn't the error be suppressed? Edit: Tested in virgin script: ``` $a = array(); $b = @$a['f5']; ``` Suppressed the error. So I'm thinking we're changing the error_reporting value somehow. (Film at 11) Thx for yr help.

Original source