Re: undefine variable

From: Date: Sun, 17 Aug 2008 21:26:08 +0000
Subject: Re: undefine variable
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-278251@lists.php.net to get a copy of this message
Hi Michael, It seems that you are trying to use the $selected_state variable in the code, but it hasn't been declared yet. To test if a variable exists, you can use the isset() function. Also, there is a quick way of writing your if/else check on one line. Here's the changes: foreach($state_list as $key=>$value) { $sel = (isset($selected_state) && $selected_state == $key)?'selected="selected"':''; echo "<option $sel value=\"$key\">$value</option>"; } echo '</select>'; ?> (That's only two lines there inside the loop, just incase my email client breaks the line because of wordwrap.) Obviously, if your code worked before, but now is not, it might be that some other change in your code is causing this error, and that introducing my change will only mask a larger logic error. You should go through your code to check where the $selected_state variable is meant to be declared to make sure it really is. There is another explanation. If you are not in control of your hosting, it could be that they changed the error reporting level to show notices as well, which I believe aren't turned on by default on all hosting packages. Ash www.ashleysheridan.co.uk

« previous php.general (#278251) next »