list explode and undefined references

From: Date: Mon, 24 May 2004 19:13:15 +0000
Subject: list explode and undefined references
Groups: php.general 
Request: Send a blank email to php-general+get-186836@lists.php.net to get a copy of this message
I came across an interesting problem today which I have found a work around but was wondering if anyone on the list had a better way to solve this problem. Been writing some php scripts on a Linux system using PHP 4.2.2. Basic script works just fine there and does what is needed. Moved this script over to a system running PHP 4.3.6 (and subsequently to one running PHP 4.3.4) and found the script dumping a tremendous number of warning messages. Tracked this down to my use of arrays and the list and explode functions. working example code follows: <?php $arrayvalues = array("alpha" => 1, "beta" => 2, "gamma" => 3); $data = array("alpha=first", "junk", "beta=last"); $numvalues = count($data); for($i=0;$i < $numvalues;$i++) { @list($parameter, $value) = explode("=",$data[$i]); if(isset($arrayvalues[$parameter])) { switch($arrayvalues[$parameter]) { case $arrayvalues["alpha"]: // something useful echo"test 1\n"; break; case $arrayvalues["beta"]: // do something else echo"test 2\n"; break; case $arrayvalues["gamma"]: // do more stuff echo"test 3\n"; break; default: // should never get here echo"test 4\n"; break; } } } ?> The code above works as needed. The problems I had to sort out however was how to suppress error messages from the list() function since in one case there is no second parameter to store in the list variables. The behavior between php 4.2.2 and php 4.3.x was different in this, 4.2.2 does not issue any warnings on that statement (without the @). Similarly I had to add a test [the if(isset())] before letting the switch statement evaluate the array value. Without that check there is an error issued and the default statement in the switch is executed. Which is kind of what I expected (except for the error). Again 4.2.2 does not issue any kind of warnings or errors on this. The errors being reported under PHP 4.3.x are: if the @ is removed: PHP Notice: Undefined offset: 1 in /home/scot/work/testing/testone.php on line 11 offset 1 has a value but in the one instance there is no offset 1 created by the explode function. and if the isset test is removed: PHP Notice: Undefined index: junk in /home/scot/work/testing/testone.php on line 15 There is no "junk" index into the arrayvalues array. I understand why these warnings are being issued, in both cases I attempted to reference an element that does not exist or has not been declared previously. I was wondering if there is a better way to handle the error message from the list() function other than just ignoring it. Or going overboard and creating my own error handling functions. -- Scot L. Harris <webid@cfl.rr.com>

« previous php.general (#186836) next »