Page 1 of 1

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

Posted: Thu Jun 02, 2005 5:17 am
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

Posted: Thu Jun 02, 2005 5:19 am
by mhouldridge
sorry,

Ive sorted it jsut by doing two else ifs..

Posted: Thu Jun 02, 2005 6:44 am
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() ;)

Posted: Thu Jun 02, 2005 9:57 am
by pickle
d11wtq wrote: By the way in PHP unlike some other languages you use

elseif() not else if() ;)
however, else if() does work

Posted: Thu Jun 02, 2005 11:15 am
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

Posted: Thu Jun 02, 2005 6:41 pm
by Skara
pickle wrote:however, else if() does work
Yeah, but not because it's the same as elseif. :P Which is kinda weird..

Posted: Thu Jun 02, 2005 7:14 pm
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?

Posted: Fri Jun 03, 2005 1:28 am
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.

Posted: Fri Jun 03, 2005 1:38 am
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..