PHP Conditionals
This page teaches you about PHP conditionals.
Conditionals are statements which tests for a certain condition, then does one thing if a certain condition is met, another thing if another condition is met, or something else if any condition is not met. The 3 main conditionals in PHP are if, else, and elseif. The syntax for a conditional is below.IF statement
Syntax
if (condition)if condition is true then do this;
PHP page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php $name="dave"; if ($name=="dave") echo "Hi Dave."; ?> </body> </html> |
||
This is how it looks
|
Hi Dave. |
||
ELSE statement
PHP page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php $name="dave"; if ($name=="brian") echo "Hi Brian."; else echo "You aren't dave."; ?> </body> </html> |
||
This is how it looks
|
You aren't Dave. |
||
ELSEIF statement
Syntax
if (condition)if condition is true then do this;
elseif
if condition is false then do this;
else
if condition is false then do this;
PHP page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php $name="joe"; if ($name=="dave") echo "Hi Dave."; elseif ($name=="brian") echo "Hi Brian."; else echo "You aren't Dave or Brian."; ?> </body> </html> |
||
This is how it looks
|
You aren't Dave or Brian. |
||
Bookmark this page: |








