On Mon, 2009-01-12 at 15:15 -0500, Frank Stanovcak wrote:
> I've googled, and found some confusing answers.
> I've tried searching the history of the news group, and only found info on
> switch or elseif seperately. :(
>
> Strictly from a performance stand point, not preference or anything else, is
> there a benefit of one over the other?
>
>
> for($i=0;$i<3;$i++){
> switch($i){
> case 0:
> header pg1 code
> break;
> case 1:
> header pg2 code
> break;
> case 3:
> header pg3 code
> break;
> };
> };
>
>
> or would that be better served using an if...elseif structure?
In some caes you can use a switch statement to avoid redundant code by
allowing a particular case to contain code for one condition then
allowing fall through to the next condition's code. The following is a
lame example:
<?php
switch( $foo )
{
case 0:
{
// something
break;
}
case 2:
{
// something else
}
case 3:
{
// something elser
break;
}
default:
{
// something defaulty
}
}
?>
In the above exmaple case 2 runs the code within it's block AND the code
within case 3's block. Using else you would probably do one of the
following:
<?php
else
if( $foo == 2 || $foo == 3 )
{
// something else
if( $foo == 3 )
{
// something elser
}
}
?>
Or with code redundancy:
<?php
else
if( $foo == 2 )
{
// something else
// something elser
}
else
if( $foo == 3 )
{
// something elser
}
?>
One has to wonder about the readability of the case version though since
one may not notice immediately the missing break statement.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php