[ciapug] explode goes boom?
Carl Olsen
carl-olsen at mchsi.com
Fri Mar 17 17:00:09 CST 2006
How about a try/catch block?
Or, test for the '|' before doing an explode.
Carl
-----Original Message-----
From: ciapug-bounces at cialug.org [mailto:ciapug-bounces at cialug.org] On Behalf
Of Chris Van Cleve
Sent: Friday, March 17, 2006 2:51 PM
To: ciapug at ciapug.org
Subject: [ciapug] explode goes boom?
Alright, here's another one of those "I brought my code into PHP 5
from PHP 4 and now I get errors!" things. Granted, this just throws a
PHP_NOTICE, but I hate log bloat so I'm looking to reduce it. THe
code snippet in question follows (part of a forms class):
// Check for <select> options
$selOptions = '';
if(count($options)>0)
{
foreach($options as $value=>$text)
{
if(empty($value) || empty($text))
{
throw new Exception('Invalid value or text for <' . $type . '>
element');
}
$value = explode('|',$value);
$value = ($value[1] == 'selected') ? $value[0] . '"
selected=true"' : implode('',$value);
$selOptions .= '<option value="' . $value . '">' . $text . '</
option>';
}
}
The $options var is an array of key=>value pairs. The bottom segment
is there to parse the key for |selected to set an option in a select
list as the default. The code works fine, but throws the following in
my apache log for every option without |selected in the key (edited
to preserve sanity):
[error] PHP Notice: Undefined offset: 1 in forms.inc.php on line ##
I understand WHY it's throwing the error. What I'm stuck on is
finding an elegant way to get around it. ;)
Any suggestions welcome.
Chris VC
P.S. Subversion beats the pants off of CVS and a three tiered
delivery system using it as the SCM is absolutely the best solution
available today! I'm loving it! Thanks for everyone's input on that.
_______________________________________________
ciapug mailing list
ciapug at cialug.org
http://cialug.org/mailman/listinfo/ciapug
More information about the ciapug
mailing list