Filed under: PHP
The If…Else Statement
You can use this if you want to execute a code if the condition is true or a different code if the condition is false. Here is the syntax
if (condition)
the code you want to be executed if the condition is true
else
the code you want to be executed if the condition is false
Here is an example
This will output “You are Bryan.” if the value of $name is Bryan, otherwise it will output “You are not Bryan.”.
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
echo “You are Bryan.”;
else
echo “You are not Bryan.”;
?>
</body>
</html>
If the code you want to be executed contains more than one line then you should enclose it with curly braces like this:
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
{
echo “You are Bryan.”;
echo “<br /> Hi!”;
}
else
echo “You are not Bryan.”;
?>
</body>
</html>
The ElseIf Statement
If the code you want to be executed is more than 2 then use the ElseIf Statement. Below is the syntax
if (condition)
the code you want to be executed if the condition is true
elseif (condition)
the code you want to be executed if the condition is true
else
the code you want to be executed if the condition is false
Here is an example:
This will output “You are Bryan.” if the value of $name is “Bryan” and “You are Joseph.” if the value of $name is “Joseph”, otherwise it will output “You are not Bryan.”.
<html>
<body>
<?php
$name = “Bryan”;
if ($name == “Bryan”)
echo “You are Bryan.”;
elseif ($name == “Joseph”)
echo “You are Joseph.”;
else
echo “You are not Bryan.”;
?>
</body>
</html>
No Comments Yet so far
Leave a comment
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>