note 126985 deleted from control-structures.if by crell
| From: | crell@php.net | Date: | Tue, 12 Apr 2022 22:42:09 +0000 |
| Subject: | note 126985 deleted from control-structures.if by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218246@lists.php.net to get a copy of this message | ||
Note Submitter: MHSarmadi
----
You have two ways to use conditions in php.
1:
<?php
if ($condition1) {
// code if $condition1 is true
} elseif ($condition2) {
// code if $condition2 is true
// note that I don't use space between "else" and "if"!
// you are possible to use space ("else if") ONLY in this syntax
// it works because you are using TWO if's.
// if (...) {
// ...
// } else {
// if (...) {
// ...
// }
// }
} else {
// code if all of conditions are false
}
?>
Second syntax is better for combining PHP and HTML.
Because you can also use this syntax for "FOR", "FOREACH", and
"SWITCH" statements.
And it lets you read your code easily.
2:
<?php
if ($condition1):
// code if $condition1 is true
?> some html codes... <?php
elseif ($condition2):
// code if $condition2 is true
?> some html codes... <?php
// note that you CANNOT use "else if" in this syntax!
// it means you MUST use "elseif".
else:
// code if both $condition1 and $condition2 are false
endif; // as you see, if you have a lot of statements, you can read them easily
?>