Re: elseif statements

From: Date: Tue, 05 May 2009 14:13:28 +0000
Subject: Re: elseif statements
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-292276@lists.php.net to get a copy of this message
On Tue, 2009-05-05 at 10:05 -0400, Robert Cummings wrote: > On Tue, 2009-05-05 at 09:49 -0400, tedd wrote: > > At 2:57 PM -0400 5/4/09, Gary wrote: > > >I am trying to get this to work, however it only reads the second if > > >statement. I get no error messages, but as I change counties, the % stays > > >the same. > > > > > >Can someone enlighten me, or should I be looking at switch statements? > > > > > >Thanks for your help. > > > > > >Gary > > > > In my opinion, never use elseif -- I've never used it. I don't see > > any reason whatsoever to do so. > > > > In my opinion, whenever your choices exceed two, use switch. > > That's some of the worst advice I've ever seen. Just so we all know why... <?php $values = array( '', 0, null, '0' ); foreach( $values as $value ) { if( $value === '' ) { echo 'If/ElseIf: The empty string'."\n"; } else if( $value === 0 ) { echo 'If/ElseIf: The integer 0'."\n"; } else if( $value === null ) { echo 'If/ElseIf: The null value'."\n"; } else if( $value === '0' ) { echo 'If/ElseIf: The string value 0'."\n"; } switch( $value ) { case '': { echo 'Switch/Case: The empty string'."\n"; break; } case 0: { echo 'Switch/Case: The integer 0'."\n"; break; } case null: { echo 'Switch/Case: The null value'."\n"; break; } case '0': { echo 'Switch/Case: The string value 0'."\n"; break; } } echo "\n"; } ?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP

« previous php.general (#292276) next »