Forum Moderators: coopster

Message Too Old, No Replies

if, then, and else

need a bit of help

         

KrazyKid

12:49 pm on May 6, 2005 (gmt 0)

10+ Year Member



hi. two questions:

1) How to I add an else statement to a switch?
2) How do I use if, then statements?
thanks :D

anshul

12:52 pm on May 6, 2005 (gmt 0)

10+ Year Member



There is no if ..then in PHP. You can use if else statements in case: of switch ..case statement.

coho75

1:21 pm on May 6, 2005 (gmt 0)

10+ Year Member



if(condition) {

perform this

} else {

perform this

}

tmiller04

1:48 pm on May 6, 2005 (gmt 0)

10+ Year Member



I'm not sure I completely understand your question, but if you are trying to get the "else" functionality out of a switch() statement... you are looking for the "default:" case.


switch($color)
{
case 'red': echo "red<BR>"; break;
case 'blue': echo "blue<BR>"; break;
default: echo "You did not enter blue or red!<BR>"; break;
}

Hope that is some help.

KrazyKid

6:55 pm on May 6, 2005 (gmt 0)

10+ Year Member



thanks. it all works :)

Sarah Atkinson

8:50 pm on May 6, 2005 (gmt 0)

10+ Year Member



also

if (condition):

ifelse (condition:

endif;

or

if (condition)
single statement;
else
singlestatment;

and this one has another name but I don't know what it is.

$veriable = (condition)? (true statement) : (false statment);

anshul

7:12 am on May 7, 2005 (gmt 0)

10+ Year Member



> I don't know what it is.
$a=9;
$b=8;
$true_or_false=($a>$b)? ($answer="a &gt; b") : ($answer="a!&gt; b");
echo $true_or_false;
echo "<br />";
echo $answer;

echo "<br />";

$a=5;
$b=9;
$true_or_false=($a>$b)? ($answer="a &gt; b") : ($answer="a!&gt; b");
echo $true_or_false;
echo "<br />";
echo $answer;


Now, you know.

Stormfx

7:22 am on May 7, 2005 (gmt 0)

10+ Year Member



Actually, I think she meant she didn't know what it was called :)

That's the ternary operator ;)