Re: Multiple "if()" statements
| From: | David Tulloh | Date: | Thu, 29 Jun 2006 00:02:33 +0000 |
| Subject: | Re: Multiple "if()" statements | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-238874@lists.php.net to get a copy of this message | ||
Grae Wolfe - PHP wrote:
> ...
> want. Any help would be great!
>
>
> if($row[1]="none") {
> print("<tr>");
> print("<td>$row[0] $row[2]</td>");
> print("</tr>");
> } else
> if($row[1]=$row[2]) {
> print("<tr>");
> print("<td>$row[0] $row[2]</td>");
> print("</tr>");
> } else
> print("<tr>");
> print("<td>$row[0] ($row[1]) $row[2]</td>");
> print("</tr>");
>
Indenting is your friend, indented version of what you had.
if($row[1]="none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
if($row[1]=$row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
The bigest problem with the above is that both the else becomes unclear
when they finish due to the lack of {}.
The ifs should also be using an == instead of an =, you want to compare
not assign.
I'm also going to throw in an elseif for fun, to get this (hopefully)
improved version:
if($row[1] == "none") {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} elseif($row[1] == $row[2]) {
print("<tr>");
print("<td>$row[0] $row[2]</td>");
print("</tr>");
} else {
print("<tr>");
print("<td>$row[0] ($row[1]) $row[2]</td>");
print("</tr>");
}