note 126984 added to control-structures.if
| From: | MHSarmadi at main dot php dot net | Date: | Tue, 12 Apr 2022 22:32:00 +0000 |
| Subject: | note 126984 added to control-structures.if | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218242@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 94.130.124.121)
----
Manual Page -- https://php.net/manual/en/control-structures.if.php
Edit -- https://main.php.net/note/edit/126984
Del: integrated -- https://main.php.net/note/delete/126984/integrated
Del: useless -- https://main.php.net/note/delete/126984/useless
Del: bad code -- https://main.php.net/note/delete/126984/bad+code
Del: spam -- https://main.php.net/note/delete/126984/spam
Del: non-english -- https://main.php.net/note/delete/126984/non-english
Del: in docs -- https://main.php.net/note/delete/126984/in+docs
Del: other reasons-- https://main.php.net/note/delete/126984
Reject -- https://main.php.net/note/reject/126984
Search -- https://main.php.net/manage/user-notes.php