PHP/Statement/If statement — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 07:07, 26 мая 2010
Содержание
- 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:
<?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";
}
?>
An if Statement
<html>
<head><title>An if Statement</title></head>
<body>
<div>
<?php
$satisfied = "very";
if ( $satisfied == "very" ) {
print "very";
}
?>
</div>
</body>
</html>
An if Statement for string
<html>
<head>
<title>An if Statement</title>
</head>
<body>
<?php
$mood = "happy";
if ( $mood == "happy" ){
print "I"m in a good mood";
}
?>
</body>
</html>
An if Statement That Uses else
<html>
<head>
<title>An if Statement That Uses else</title>
</head>
<body>
<div>
<?php
$satisfied = "very";
if ( $satisfied == "very" ) {
print "very";
} else {
print "not very";
}
?>
</div>
</body>
</html>
An if statement That Uses else and else if
<html>
<head>
<title>An if statement That Uses else and else if</title>
</head>
<body>
<div>
<?php
$satisfied = "no";
if ( $satisfied == "very" ) {
print "very";
} else if ( $satisfied == "no") {
print "no";
// request further feedback
} else {
print "else";
}
?>
</div>
</body>
</html>
Basic Use of the if Statement
<?php
if (true) echo "This will always display!<BR>";
if (false) {
echo "This will never, ever be displayed.<BR>";
} else {
echo "This too will always display!<BR>";
}
?>
Case Switching
<?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";
}
?>
Checking multiple conditions
<?
if ($username == "Admin"){
echo ("Welcome to the admin page.");
}
elseif ($username == "Guest"){
echo ("Please take a look around.");
}
else {
echo ("Welcome back, $username.");
}
?>
Executing Multiple Statements with One if
<?
$sky_color = "blue";
if($sky_color == "blue") {
print("A <br/>");
print("B <br/>");
}
print("Here I am.");
?>
If-else Statement
<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>
In if statement use OR to connect two conditions
<?
$degrees = "95";
$hot = "yes";
if (($degrees > 100) || ($hot == "yes")) {
echo "<P>TESTIt"s <strong>really</strong> hot!</P>";
} else {
echo "<P>TESTIt"s bearable.</P>";
}
?>
Making a decision with if()
<?
$logged_in = true;
if ($logged_in) {
print "Welcome aboard, trusted user.";
}
?>
Multiconditional if Statements
<?php
$finished = true;
$complete = 14;
if ( ($complete >= 15) || $finished)) {
echo "get some sleep!<BR>";
} else {
echo "no sleep tonight!<BR>";
}
?>
Multiple statements in an if() code block
<?
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.";
?>
Print the string "Child message" if the $age variable is between 1 and 17
<?
$age = 12;
if ( $age >= 18 && $age <= 35 ) {
print "Youth message<br />\n";
} else if ( $age >= 1 && $age <= 17 ) {
print "Child message<br />\n";
} else {
print "Generic message<br />\n";
}
?>
Use and to connect two statement in if statement
<?
$degrees = "95";
$hot = "yes";
if (($degrees > 100) && ($hot == "yes")) {
echo "<P>TESTIt"s <strong>really</strong> hot!</P>";
} else {
echo "<P> TESTIt"s bearable.</P>";
}
?>
Using elseif()
<?
$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.";
}
?>
Using else with if()
<?
$logged_in = true;
if ($logged_in) {
print "Welcome aboard, trusted user.";
} else {
print "Howdy, stranger.";
}
?>
Using if Statements to Mimic a select Statement
<?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;
}
?>
Using if statement to print "Youth message" if $age is between 18 and 35
<?
$age = 22;
if ( $age >= 18 && $age <= 35 ) {
print "Youth message<br />";
} else {
print "Generic message<br/>";
}
?>
Using if to test for multiple values
<?
$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.";
}
?>
Within an if Else statement
<?
$a = 21;
$b = 15;
echo "<P>Original value of \$a is $a and \$b is $b</P>";
if ($a == $b) {
echo "<P>TEST\$a equals \$b</P>";
} else {
echo "<P>TEST\$a is not equal to \$b</P>";
}
?>