PHP/Statement/If statement
Содержание
- 1 A helpful addition to if statements is the elseif statement, which allows you to chain conditions together in a more intelligent way:
- 2 An if Statement
- 3 An if Statement for string
- 4 An if Statement That Uses else
- 5 An if statement That Uses else and else if
- 6 Basic Use of the if Statement
- 7 Case Switching
- 8 Checking multiple conditions
- 9 Executing Multiple Statements with One if
- 10 If-else Statement
- 11 In if statement use OR to connect two conditions
- 12 Making a decision with if()
- 13 Multiconditional if Statements
- 14 Multiple statements in an if() code block
- 15 Print the string "Child message" if the $age variable is between 1 and 17
- 16 Use and to connect two statement in if statement
- 17 Using elseif()
- 18 Using else with if()
- 19 Using if Statements to Mimic a select Statement
- 20 Using if statement to print "Youth message" if $age is between 18 and 35
- 21 Using if to test for multiple values
- 22 Within an if Else statement
A helpful addition to if statements is the elseif statement, which allows you to chain conditions together in a more intelligent way:
<source lang="html4strict">
<?php
if ($Age < 10) { print "You"re under 10"; } elseif ($Age < 20) { print "You"re under 20"; } elseif ($Age < 30) { print "You"re under 30"; } elseif ($Age < 40) { print "You"re under 40"; } else { print "You"re over 40"; } ?> </source>
An if Statement
<source lang="html4strict">
<html> <head><title>An if Statement</title></head> <body>
<?php
$satisfied = "very"; if ( $satisfied == "very" ) { print "very"; }
?>
</body> </html>
</source>
An if Statement for string
<source lang="html4strict">
<html> <head> <title>An if Statement</title> </head> <body> <?php $mood = "happy"; if ( $mood == "happy" ){
print "I"m in a good mood";
} ?> </body> </html>
</source>
An if Statement That Uses else
<source lang="html4strict">
<html> <head> <title>An if Statement That Uses else</title> </head> <body>
<?php
$satisfied = "very"; if ( $satisfied == "very" ) { print "very"; } else { print "not very"; }
?>
</body> </html>
</source>
An if statement That Uses else and else if
<source lang="html4strict">
<html> <head> <title>An if statement That Uses else and else if</title> </head> <body>
<?php
$satisfied = "no"; if ( $satisfied == "very" ) { print "very"; } else if ( $satisfied == "no") { print "no"; // request further feedback } else { print "else"; }
?>
</body> </html>
</source>
Basic Use of the if Statement
<source lang="html4strict">
<?php
if (true) echo "This will always display!
"; if (false) { echo "This will never, ever be displayed.
"; } else { echo "This too will always display!
"; }
?>
</source>
Case Switching
<source lang="html4strict">
<?php
$Name = "Bob"; if ($Name = = "Jim") { print "Your name is Jim\n"; } elseif ($Name = = "Bob") { print "Your name is Bob\n"; } else { print "I don"t know your name!\n"; } ?> </source>
Checking multiple conditions
<source lang="html4strict">
<? if ($username == "Admin"){
echo ("Welcome to the admin page.");
} elseif ($username == "Guest"){
echo ("Please take a look around.");
} else {
echo ("Welcome back, $username.");
} ?>
</source>
Executing Multiple Statements with One if
<source lang="html4strict">
<? $sky_color = "blue"; if($sky_color == "blue") {
print("A
"); print("B
");
} print("Here I am."); ?>
</source>
If-else Statement
<source lang="html4strict">
<html>
<head> <title>If-else Statement</title> </head> <body> <?php $num = 2; $bool=false; if($num==1 and $bool==true) echo("Test 1 success"); else if($num==2 and $bool==true) echo("Test 2 success"); else if($num==2 and $bool==false) echo("Test 3 success"); else if($num==3 and $bool==false) echo("Test 4 success"); ?> </body>
</html>
</source>
In if statement use OR to connect two conditions
<source lang="html4strict">
<? $degrees = "95"; $hot = "yes"; if (($degrees > 100) || ($hot == "yes")) {
echo "TESTIt"s really hot!
";} else {
echo "TESTIt"s bearable.
";} ?>
</source>
Making a decision with if()
<source lang="html4strict">
<? $logged_in = true; if ($logged_in) {
print "Welcome aboard, trusted user.";
} ?>
</source>
Multiconditional if Statements
<source lang="html4strict">
<?php
$finished = true; $complete = 14; if ( ($complete >= 15) || $finished)) { echo "get some sleep!
"; } else { echo "no sleep tonight!
"; }
?>
</source>
Multiple statements in an if() code block
<source lang="html4strict">
<? print "This is always printed."; $logged_in = true; if ($logged_in) {
print "Welcome aboard, trusted user."; print "This is only printed if $logged_in is true.";
} print "This is also always printed."; ?>
</source>
Print the string "Child message" if the $age variable is between 1 and 17
<source lang="html4strict">
<? $age = 12; if ( $age >= 18 && $age <= 35 ) {
print "Youth message
\n";
} else if ( $age >= 1 && $age <= 17 ) {
print "Child message
\n";
} else {
print "Generic message
\n";
} ?>
</source>
Use and to connect two statement in if statement
<source lang="html4strict">
<? $degrees = "95"; $hot = "yes"; if (($degrees > 100) && ($hot == "yes")) {
echo "TESTIt"s really hot!
";} else {
echo "TESTIt"s bearable.
";} ?>
</source>
Using elseif()
<source lang="html4strict">
<? $logged_in = true; if ($logged_in) {
print "Welcome aboard, trusted user.";
} elseif ($new_messages) {
print "Dear stranger, there are new messages.";
} elseif ($emergency) {
print "Stranger, there are no new messages, but there is an emergency.";
} ?>
</source>
Using else with if()
<source lang="html4strict">
<? $logged_in = true; if ($logged_in) {
print "Welcome aboard, trusted user.";
} else {
print "Howdy, stranger.";
} ?>
</source>
Using if Statements to Mimic a select Statement
<source lang="html4strict">
<?php
$i = 0; if($i == 0) echo "First case"; if($i == 1) echo "Second case"; switch($i) { case 0: echo "First case"; break; case 1: echo "Second case"; break; }
?>
</source>
Using if statement to print "Youth message" if $age is between 18 and 35
<source lang="html4strict">
<? $age = 22; if ( $age >= 18 && $age <= 35 ) {
print "Youth message
";
} else {
print "Generic message
";
} ?>
</source>
Using if to test for multiple values
<source lang="html4strict">
<? $action = "ADD"; if ($action == "ADD") {
echo "Perform actions for adding.";
} elseif ($action == "MODIFY") {
echo "Perform actions for modifying.";
} elseif ($action == "DELETE") {
echo "Perform actions for deleting.";
} ?>
</source>
Within an if Else statement
<source lang="html4strict">
<? $a = 21; $b = 15;
echo "Original value of \$a is $a and \$b is $b
";if ($a == $b) {
echo "TEST\$a equals \$b
";} else {
echo "TEST\$a is not equal to \$b
";} ?>
</source>