RE: if/elseif being treated as if/if

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2010-09-24 at 15:44 -0400, Steve Staples wrote:

> this would be the same as:
> (commented below)
> 
> On Fri, 2010-09-24 at 15:30 -0400, tedd wrote:
> > At 2:23 PM -0400 9/24/10, Bob McConnell wrote:
> > >
> > >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
> > 
> > Bob:
> > 
> > Not so, O'wise one.
> > 
> > This will work:
> > 
> > switch(1)
> >     {
> >     case $a > $b:
> if($a > $b)
> >     /* whatever
> >     break;
> > 
> elseif ($c == 1)
> >     case $c == 1:
> >     /* whatever
> >     break;
> > 
> elseif($d == 'this works')
> >     case $d == 'this works':
> >     /* whatever
> >     break;
> >     }
> > Granted, it's not the normal way a switch works in some other 
> > languages, but it does work in PHP.  :-)
> > 
> 
> All you have to remember, and same as with this switch, is that the
> first match, will stop processing the rest of the stuff.
> 
> Steve.
> 
> 
> 
> 
> > Cheers,
> > 
> > tedd
> > 
> > -- 
> > -------
> > http://sperling.com/
> > 
> 
> 
> 



Actually, processing only stops at the first break statement, so in this
example, it would run through to case 3:

$var = 1;
switch($var)
{
    case 1:
    {
        echo 1;
    }
    case 2:
    {
        echo 2;
    }
    case 3:
    {
        echo 3;
        break;
    }
    case 4:
    {
        echo 4;
        break;
    }
    default:
    {
        echo 'default';
    }
}

This would display '123' in the output.

Thanks,
Ash
http://www.ashleysheridan.co.uk



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux