help - I do not know how to do a multiple if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

help - I do not know how to do a multiple if statement

Post by mhouldridge »

Hi,

I have an if statement which works below;

Code: Select all

for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
	$stopped = $row['stopped'];
	$void = $row['title'];
		if($stopped =='yes'){
		echo "<TR bgcolor=\"#C1FFD1\">\n";
		}
	else if($i % 2) {
		echo "<TR bgcolor=\"#ffff99\">\n";    
		} 
	else {         
		echo "<TR bgcolor=\"#ffffcc\">\n";    
		}

However tis does not work. I need to add a fourth if;

Code: Select all

for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from our result set
	$stopped = $row['stopped'];
	$void = $row['title'];
		if($stopped =='yes'){
		echo "<TR bgcolor=\"#C1FFD1\">\n";
		}
		if($void =='VOID'){
		echo "<tr bgcolor=\"#FFCCCC\">\n";
		}
	else if($i % 2) {
		echo "<TR bgcolor=\"#ffff99\">\n";    
		} 
	else {         
		echo "<TR bgcolor=\"#ffffcc\">\n";    
		}

Please help
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

sorry,

Ive sorted it jsut by doing two else ifs..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mhouldridge wrote:sorry,

Ive sorted it jsut by doing two else ifs..
By the way in PHP unlike some other languages you use

elseif() not else if() ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

d11wtq wrote: By the way in PHP unlike some other languages you use

elseif() not else if() ;)
however, else if() does work
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

It's obvious that else if() works but I began wondering why there is such a thing as elseif()
Only thing I can think of as reason is parser speed. elseif() is one control structure and else if() are two and that's why elseif() must be faster to parse.

My guess is that something like this MATLAB examble shows is going on inside php parser http://www.mathworks.com/access/helpdes ... lseif.html

Code: Select all

if(I'm wrong)
  Please correct me
  elseif(You agree)
  Smile
  else don't mind about me at all
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

pickle wrote:however, else if() does work
Yeah, but not because it's the same as elseif. :P Which is kinda weird..
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

else if() takes advantage of the fact that you don't need curly braces for one-line if constructs.

Code: Select all

if ($boom) {
  $one = true;
} else if ($bang) {
  $two = true;
} else if ($bing) {
  $three = true;
} else {
  $four = true;
}
Becomes, when braced,

Code: Select all

if ($boom) {
  $one = true;
} else {
  if ($bang) {
    $two = true;
  } else {
    if ($bing) {
      $three = true;
    } else {
      $four = true;
    }
  }
}
Which is a bit scary, don't you think?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

Skara wrote:
pickle wrote:however, else if() does work
Yeah, but not because it's the same as elseif. :P Which is kinda weird..
uhm... it is the same... what would be different?
only thing is that "else if" is possible due to the structure of the language... (such that if must exist and else must exist, thus maing else if exist).

elseif and else if are the very samething, just that "elseif" is recommended by reasons stated earlier.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Ambush Commander wrote: else if() takes advantage of the fact that you don't need curly braces for one-line if constructs.

Becomes, when braced
Ambush already said it..
Post Reply