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 16:08 -0400, Joshua Kehn wrote:

> On Sep 24, 2010, at 4:04 PM, Ashley Sheridan wrote:
> 
> > On Fri, 2010-09-24 at 15:54 -0400, Bob McConnell wrote:
> > 
> >> From: tedd
> >> 
> >>> 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.
> >> 
> >>> Not so, O'wise one.
> >>> 
> >>> This will work:
> >>> 
> >>> switch(1)
> >>>    {
> >>>    case $a > $b:
> >>>    /* whatever
> >>>    break;
> >>> 
> >>>    case $c == 1:
> >>>    /* whatever
> >>>    break;
> >>> 
> >>>    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.  :-)
> >> 
> >> That is just so wrong, it can't actually be taken seriously. There is
> >> simply no justification for such broken logic.
> >> 
> >> Bob McConnell
> >> 
> > 
> > 
> > I don't often use this type of logic, but I have used it before and it's
> > served me well. Essentially, a switch is a glorified if statement, and I
> > find them a lot nicer to read and write than a series of if/elseif
> > blocks.
> > 
> > 
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> > 
> > 
> 
> Proper bracing style makes everything visually appealing.
> 
> if(cond)
> {
>     /* perform */
> }
> else if(cond)
> {
>     /* action */
> }
> else if(cond)
> {
>     /* if */
> }
> else
> {
>     /* applicable to cond */
> }
> 
> Rather then
> 
> if(cond){
> 
> } else if(cond) {
> 
> } else if(cond) {
>  
> } else {
> 
> }
> 
> which may look condensed but to me doesn't follow the program flow at all.
> 
> Regards,
> 
> -Josh


That is my preferred way of coding anyway for several reasons as you can
do this without causing any problems:

//if(condition)
{
    // code here
}

whereas if you do it with the variant of this style:

// if(condition) {
    // code here
//}

You have to comment out two lines instead of one to achieve the same
result, and you only save a byte (or two if the line ending is a
carriage return and a line break) by putting the opening brace on the
first line.

The style made sense when it was used for code printouts and displaying
on a text terminal, but in these modern days of GUI editors and the
(mostly conceptual) paperless office, there seems little point.

That is all a little off-topic though.

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