From: Andy McKenzie on 24 Sep 2010 13:50 Hey folks, Here's the deal. I have the following code: if($col_vals[$i][$val['column']] == $search_result[0][$col]) { echo ' selected="selected"'; } elseif($val['default'] == $col_vals[$i][$val['column']]) { echo ' selected="selected"'; } It's supposed to check whether there's a value in the db ($search_result[0][$col]) that matches the current column value, and if not, check whether the default matches it. It does that, sort of. In fact, both statements trigger, which I would have said wasn't possible. So the question is: what causes both parts of an if/elseif statement to trigger? As far as I can see my punctuation is correct, and I've confirmed through debugging statements that all the values are what I expect, so how do I make the elseif stop acting like another if? Or, alternatively, have I just misunderstood all this time what the if/elseif statement does? Thanks, Alex
From: chris h on 24 Sep 2010 13:56 Andy I see no reason why both echo's would fire; unless this block of code gets executed multiple times. can we see more of the code? Chris H. On Fri, Sep 24, 2010 at 1:50 PM, Andy McKenzie <amckenzie4(a)gmail.com> wrote: > Hey folks, > > Here's the deal. I have the following code: > > if($col_vals[$i][$val['column']] == $search_result[0][$col]) > { echo ' selected="selected"'; } > elseif($val['default'] == $col_vals[$i][$val['column']]) > { echo ' selected="selected"'; } > > It's supposed to check whether there's a value in the db > ($search_result[0][$col]) that matches the current column value, and > if not, check whether the default matches it. It does that, sort of. > In fact, both statements trigger, which I would have said wasn't > possible. > > So the question is: what causes both parts of an if/elseif > statement to trigger? As far as I can see my punctuation is correct, > and I've confirmed through debugging statements that all the values > are what I expect, so how do I make the elseif stop acting like > another if? Or, alternatively, have I just misunderstood all this > time what the if/elseif statement does? > > Thanks, > Alex > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
From: Marc Guay on 24 Sep 2010 14:00 if(1 == 1){ echo 'here'; } elseif(1 == 1){ echo 'here"'; } Will only echo "here" once.
From: tedd on 24 Sep 2010 14:11 At 1:50 PM -0400 9/24/10, Andy McKenzie wrote: >Hey folks, > > Here's the deal. I have the following code: > >if($col_vals[$i][$val['column']] == $search_result[0][$col]) > { echo ' selected="selected"'; } >elseif($val['default'] == $col_vals[$i][$val['column']]) > { echo ' selected="selected"'; } > > It's supposed to check whether there's a value in the db >($search_result[0][$col]) that matches the current column value, and >if not, check whether the default matches it. It does that, sort of. >In fact, both statements trigger, which I would have said wasn't >possible. > > So the question is: what causes both parts of an if/elseif >statement to trigger? As far as I can see my punctuation is correct, >and I've confirmed through debugging statements that all the values >are what I expect, so how do I make the elseif stop acting like >another if? Or, alternatively, have I just misunderstood all this >time what the if/elseif statement does? > >Thanks, > Alex Alex: I am not in the majority when I say for conditions where you have more than two options use a switch control and not an elseif. In 40+ years of programming, I have never used elseif because the control confuses me. It is *much* easier for me to use, understand, and document a switch statement than an elseif. Your mileage may vary. Cheers, tedd -- ------- http://sperling.com/
From: "Bob McConnell" on 24 Sep 2010 14:23 From: tedd > At 1:50 PM -0400 9/24/10, Andy McKenzie wrote: >>Hey folks, >> >> Here's the deal. I have the following code: >> >>if($col_vals[$i][$val['column']] == $search_result[0][$col]) >> { echo ' selected="selected"'; } >>elseif($val['default'] == $col_vals[$i][$val['column']]) >> { echo ' selected="selected"'; } >> >> It's supposed to check whether there's a value in the db >>($search_result[0][$col]) that matches the current column value, and >>if not, check whether the default matches it. It does that, sort of. >>In fact, both statements trigger, which I would have said wasn't >>possible. >> >> So the question is: what causes both parts of an if/elseif >>statement to trigger? As far as I can see my punctuation is correct, >>and I've confirmed through debugging statements that all the values >>are what I expect, so how do I make the elseif stop acting like >>another if? Or, alternatively, have I just misunderstood all this >>time what the if/elseif statement does? > > Alex: > > I am not in the majority when I say for conditions where you have > more than two options use a switch control and not an elseif. > > In 40+ years of programming, I have never used elseif because the > control confuses me. It is *much* easier for me to use, understand, > and document a switch statement than an elseif. > > Your mileage may vary. A switch works when a single test can dispatch all possible branches. If you have a series of tests where each looks for a different subset of conditions, you need an elseif. Bob McConnell
|
Next
|
Last
Pages: 1 2 3 4 Prev: Heredocs, print_r() and newline "\n" and fnmatch() -curious failures ... Next: Array question |