2

i often run into a problem when variables/attributes have the wrong type. The Problem is that it is hard to trace since my PHP (5.3) just crashes, does not put out an error or even write to the error log (*1). It just crashes.

I think accessing a string like an array shouldn't be untraceable should it? I mean, PHP is not C right?

Is there a way to change this behavior or some sort of best practice to get around this problem? Apart from checking every variable everywhere all the time and therefore writing 5 times more code?

[Update]: Okay, if i simplify the code outside Zend it seems to work. It must be Zend related somehow. Though i do have all the phpSettings set in my application.ini:

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
phpSettings.error_reporting = "E_ALL|E_STRICT"

And the code causing the Error (which works in a sandbox as i just tried) is this:

$prefix = (strpos($obj[3][1], 'videometa') !== false) ? "http://www7.example.org" : "http://www.example.org";

Where $obj is a json string.

[Update 2]: So i tried echoing the php setting using ini_get right above my code and it says error_reporting is on E_ALL|E_STRICT, display_error is ON etc.

So this:

echo '<br/>ini_get = '.ini_get('display_errors').';';
echo '<br/>ini_get = '.ini_get('error_reporting').';';
echo '<br/>ini_get = '.ini_get('error_log').';';
echo '<br>$obj: ';
$obj = 'peter';
var_dump($obj);

echo '<br/>Now for the critical code:';
$prefix = (strpos($obj[MC_IMAGETYPE_VDT][1], 'videometa') !== false) ? "http://www7.example.org" : "http://www.example.org";

echo '<br/>It never shows this!';

Puts out this:

ini_get = 1;
ini_get = E_ALL|E_STRICT;
ini_get = /Applications/MAMP/logs/php_error.log;
$obj: string(5) "peter"

Now for the critical code:

And then stops. Any Ideas?

*1. php.ini has display_errors = On and error_reporting = E_ALL and so on. All good.

Andresch Serj
  • 35,217
  • 15
  • 59
  • 101
  • did you try `E_ALL|E_STRICT`? – prodigitalson Jul 19 '11 at 10:04
  • Could you post an example which runs into an error? – Marco Jul 19 '11 at 10:05
  • 2
    "I think accessing a string like an array shouldn't be untraceable should it? I mean, PHP is not C right?" - You do realize that accessing characters in a string using array subscript syntax is perfectly legal? That is, `$a = "foobar"; $b = $a[1]; echo $b;` will output `o`. – tdammers Jul 19 '11 at 10:06
  • try adding error_reporting(E_ALL); on top of your fileor error_reporting(E_ALL | E_STRICT); – Lachezar Todorov Jul 19 '11 at 10:08
  • I am still pretty interessted in an example... Dou you have one? – Marco Jul 19 '11 at 10:11
  • @FlyBy: Will edit in a minute. – Andresch Serj Jul 19 '11 at 10:11
  • Btw.. Writing good, solid and secure code always means to write some more lines. We all know that.. : ) – Marco Jul 19 '11 at 10:15
  • @FlyBy: Added the Code Example. Makes no sense to me. – Andresch Serj Jul 19 '11 at 10:37
  • 1
    Makes sense why the code stops. Makes no sense why it doesn't output an error. The code stops cause a string has only 1 dimension which you can parse. So trying to get [3][1] is truely an error. But I don't know why it doesn't output any errors... Sorry for that... – Marco Jul 19 '11 at 10:48
  • @FlyBy: I do know why it stops myself and i also do not know why it doesn't show the error. But i bet it is not your fault now is it ?;D – Andresch Serj Jul 19 '11 at 11:06
  • 1
    No it isn't! : ) By the way... If I execute I get "Fatal error: Cannot use string offset as an array in C:\www\localhost\test.php". I am using PHP 5.3.6. `error_reporting = E_ALL | E_STRICT` `display_errors = On` `display_startup_errors = On` `log_errors =On` `track_errors = On`. Remember to restart your webserver after setting the values... I guess I don't have to say that... :D – Marco Jul 19 '11 at 11:14

1 Answers1

3

I don't think the method you are using to set error reporting is correct, you are setting it to a string value when it should be numeric (or a constant such as E_ALL which has a numeric value). Try just:

phpSettings.error_reporting = E_ALL|E_STRICT

(i.e. no quotes) or:

phpSettings.error_reporting = 32767

If all else fails you could always temporarily set the error reporting value above the code that you think is causing the problem:

error_reporting(E_ALL);

also, you have:

$prefix = (strpos($obj[MC_IMAGETYPE_VDT][1], 'videometa') !== false) ? "http://www7.example.org" : "http://www.example.org";

but $obj is a string which you are trying to access as a nested array. This will give you Fatal error: Cannot use string offset as an array which is probably why your code is failing.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • Thanks a lot, that was the Problem the whole time! I somehow mixed up Constants and Strings and since it is in an ini File it didn't _look_ wrong at all. Thanks! – Andresch Serj Jul 19 '11 at 12:50