0% found this document useful (0 votes)
21 views26 pages

(Lesson 4) IF Statements

Uploaded by

aastabdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views26 pages

(Lesson 4) IF Statements

Uploaded by

aastabdelrahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PHP Control Structures:

IF Statements

Dr. Ali Allam Web Programming (BIS317E)


Quick Review on Conditional Statements

 Conditional statements (if statements) are used to select a choice or


take a decision upon a given condition.
 IF statements are based on a condition whose result is either TRUE or
FALSE.
 This condition statement simply compares two values to each other.
 The main logical operators used to compare values are:
> >= < <= == !=
Greater than Greater than Less than Less than or Equals Not equal to
or equals equals
Quick Review on Conditional Statements

 The two values compared to each other can be either a variable,


an expression, or a constant.
Example Operands used in comparison Explanation
$X > 0 Variable and Constant Check if $X is greater than 0
$Y <= $X Variable and Variable Check if $Y is less than or equal to $X
$X == $Z*10 Variable and Expression Check if $X is equal to $Z*10
60 != $A+$B*2 Constant and Expression Check if $A+$B*2 is not equal to 60
$A%$B < $Y – $X Expression and Expression Check if $A%$B is less than $Y – $X
Quick Review on Conditional Statements

 In some cases, you may need to write an IF statement having more


than one condition.
 Multiple conditions are made using either:
 condition1 && condition2 → AND
 condition1 || condition2 → OR
IF Statements: PHP Syntax

In short, PHP has the three following kinds of IF statements:


1. if statement (without else): executes some statement(s) if one condition is
true.
2. if-else statement: executes some statement(s) if a condition is true, and
executes other else statement(s) if that condition is false.
3. if-elseif statement: executes different statement(s) for more than two
conditions.
IF Statements: PHP Syntax
1. if statement (without else): executes some statement(s) if one condition is
true.
Syntax Example
if (condition) $mark = $_GET["num"];
{ if ($mark == 100)
statement(s) executed if condition is true; echo "You got the full mark";
}
Very Important Notes:
1. The if (condition) is NEVER followed by a semi-colon.
2. The curly brackets { } that follow the if condition, are mandatory if there is more
than one statement to be executed. These curly brackets are optional and can be
omitted in case there is only one single statement to be executed (as shown in the
example above).
IF Statements: PHP Syntax
2. if-else statement: executes some statement(s) if a condition is true and
other else statement(s) if that condition is false.
Syntax Example Note:
if (condition) $mark = $_GET["num"]; Curly brackets are
{ if ($mark >= 50) mandatory in this
statement(s) executed if the { example because
condition is true; echo "Your mark is $mark<br>"; there are two echo
} echo "You passed."; statements to be
else } executed. So, this
{ else block of statements
statement(s) executed if the {
are included in
condition was false; echo "Your mark is $mark<br>";
} echo "You failed."; between the curly
} brackets.
IF Statements: PHP Syntax
3. if-elseif statement: executes different statement(s) for more than two conditions.
Syntax Example
if (condition1) $x =
{ $_GET["num"];
statement(s) executed if condition1 is true; if ($x > 0)
} echo "Positive";
elseif (condition2) elseif ($x < 0)
{ echo "Negative";
statement(s) executed if condition1 was false but condition2 is true; else
} echo "Neutral";
else
{
statement(s) executed if all previous conditions were false;
}
Example (1): Decide whether a number is either
positive or negative (two choices/options)

HTML Form (form1.html) PHP Program (prog1.php)


<html> <html>
<head> <head>
<title>Input</title> <title>Output</title>
</head>
</head>
<body>
<body> <?php
<form action="prog1.php"> $x = $_GET["num"];
<h3>Enter a number</h3> if ($x>=0)
<input type="text" name="num"> echo "<b>$x is a positive number</b>";
<br> <input type="submit"> else
</form> echo "<b>$x is a negative number</b>";
?>
</body>
</body>
</html> </html>
Example (1): Decide whether a number is either
positive or negative (two choices/options)
 What would the PHP program print in the following cases:
If the user inputs 5 If the user inputs -6 If the user inputs 0
Example (2): Decide whether a number is positive,
negative, or zero (more than two choices/options)

HTML Form (form2.html) PHP Program (prog2.php)


<html> <html>
<head> <body>
<title>Input</title> <?php
$x = $_GET["num"];
</head>
if ($x>0)
<body> echo "<b>$x is a positive number</b>";
<form action="prog2.php"> elseif ($x<0)
<h3>Enter a number</h3> echo "<b>$x is a negative number</b>";
<input type="text" name="num"> else
<br> <input type="submit"> echo "<b>$x is a neutral number</b>";
</form> ?>
</body>
</body>
</html>
</html>
Example (2): Decide whether a number is positive,
negative, or zero (more than two choices/options)
 What would the PHP program print in the following cases:
If the user inputs 5 If the user inputs -6 If the user inputs 0
Example (3): Get the maximum value of two
numbers
HTML Form (form3.html) PHP Program (prog3.php)
<html> <html>
<head> <body>
<title>Input</title> <?php
$x = $_GET["num1"];
</head>
$y = $_GET["num2"];
<body> if ($x>$y)
<form action="prog3.php"> echo "The maximum number is $x";
<h3>Enter two numbers:</h3> elseif ($y>$x)
<input type="text" name="num1"> echo "The maximum number is $y";
<input type="text" name="num2"> else
<br> <input type="submit"> echo "The two numbers are equal, and
their value is $x";
</form>
?>
</body> </body>
</html> </html>
Example (4): Decide whether a number is
even or odd

HTML Form (form4.html) PHP Program (prog4.php)


<html> <html>
<head> <head>
<title>Input</title> <title>Output</title>
</head>
</head>
<body>
<body> <?php
<form action="prog4.php"> $x = $_GET["num"];
<h3>Enter a number</h3> if ($x%2==0)
<input type="text" name="num"> echo "<b>$x is an even number</b>";
<br> <input type="submit"> else
</form> echo "<b>$x is an odd number</b>";
?>
</body>
</body>
</html> </html>
Example (5): Print the corresponding grade of the
GPA in specific colors

Input: GPA Output: Corresponding Grade and Color


3.4 – 4.0 Excellent
2.8 – 3.39 Very Good
2.4 – 2.79 Good
2.0 – 2.39 Pass
0.0 – 1.99 Fail
Otherwise Invalid GPA
Example (5): Print the corresponding grade of the
GPA in specific colors <html>
<body>
<?php
$x = $_GET["gpa"];
if ($x>=3.4 && $x<=4.0)
<html> echo"<p style=color:green>Excellent</p>";
elseif ($x>=2.8 && $x<=3.39)
<head> echo"<p style=color:purple>Very Good</p>";
<title>Input</title> elseif ($x>=2.4 && $x<=2.79)
</head> echo"<p style=color:orange>Good</p>";
<body> elseif ($x>=2.0 && $x<=2.39)
<form action="prog5.php"> echo"<p style=color:blue>Pass</p>";
<h3>Enter your GPA</h3> elseif ($x>=0 && $x<=1.99)
<input type="text" name="gpa"> echo"<p>Fail</p>";
else
<br> <input type="submit"> echo"<p style=color:red>Invalid GPA</p>";
</form> ?>
</body> </body>
</html> </html>

HTML Form (form5.html) PHP Program (prog5.php)


Let’s take the previous example again, using
radio buttons to choose your GPA range,
instead of typing your GPA in a textbox.
Example (6): Print the corresponding grade of the
GPA in specific colors
HTML Form (form6.html)
<html>
<body>
<form action="prog6.php">
<h3>Choose your GPA Range</h3>
<input type="radio" name="gpa" value="1" checked> 3.4 – 4.0 <br>
<input type="radio" name="gpa" value="2"> 2.8 – 3.39 <br>
<input type="radio" name="gpa" value="3"> 2.4 – 2.79 <br>
<input type="radio" name="gpa" value="4"> 2.0 – 2.39 <br>
<input type="radio" name="gpa" value="5"> 0.0 – 1.99 <br>
<input type="submit">
</form>
</body>
</html>
Example (6): Print the corresponding grade of the
GPA in specific colors
<html>
<body>
<?php
$x = $_GET["gpa"]; PHP Program (prog6.php)
if ($x==1)
echo"<p style=color:green>Excellent</p>";
elseif ($x==2)
echo"<p style=color:purple>Very Good</p>";
elseif ($x==3) Did you notice the difference in
echo"<p style=color:orange>Good</p>"; handling the input, using radio
elseif ($x==4)
echo"<p style=color:blue>Pass</p>"; buttons instead of a textbox?
elseif ($x==5)
echo"<p>Fail</p>";
?>
</body>
</html>
Let’s take the same example again, using a drop-
down list to choose your GPA range.
Note: Drop-down lists and radio buttons are
handled in the same exact way.
Example (7): Print the corresponding grade of the
GPA in specific colors
<html>
<body>
<form action="prog7.php">
<h3>Choose your GPA Range</h3>
<select name="gpa">
<option value="1" selected> 3.4 – 4.0 </option>
<option value="2"> 2.8 – 3.39 </option> HTML Form (form7.html)
<option value="3"> 2.4 – 2.79 </option>
<option value="4"> 2.0 – 2.39 </option>
<option value="5"> 0.0 – 1.99 </option>
</select>
<input type="submit">
</form>
</body>
</html>
Example (7): Print the corresponding grade of the
GPA in specific colors
<html>
<body>
<?php
$x = $_GET["gpa"]; PHP Program (prog7.php)
if ($x==1)
echo"<p style=color:green>Excellent</p>";
elseif ($x==2)
echo"<p style=color:purple>Very Good</p>";
elseif ($x==3) Using a drop-down list instead of
echo"<p style=color:orange>Good</p>"; radio buttons made no difference at
elseif ($x==4)
echo"<p style=color:blue>Pass</p>"; all in the PHP script.
elseif ($x==5)
echo"<p>Fail</p>";
?>
</body>
</html>
IF Statements are also very useful in validating input
data (i.e. ensure that the input data is entered properly).
Ex: The username, password, and password confirmation must NOT
be empty (all fields are mandatory). Also, the password and its
confirmation must be the same.
Example (8): Validate a registration form –
username, password, and its confirmation.
HTML Form (form8.html)
<html>
<body>
<form action="prog8.php" method="post">
Username: <input type="text" name="user"><br>
Password: <input type="password" name="pwd"><br>
Confirm Password: <input type="password" name="pwd2"><br>
<input type="submit" value="Register"><input type="reset">
</form>
</body>
</html>
Example (8): Validate a registration form –
username, password, and its confirmation.
<html> <body style="color:red; font-size:large">
<?php
$user = $_POST["user"];
$pwd = $_POST["pwd"];
$pwd2 = $_POST["pwd2"];
if ($user == "")
echo "Invalid entry. Username is missing!"; PHP Program (prog8.php)
elseif ($pwd == "")
echo "Invalid entry. Password is missing!";
elseif ($pwd2 == "")
echo "Invalid entry. Password is not confirmed!";
elseif($pwd != $pwd2)
echo "Error. Password mismatch!";
else
echo "Successfully Registered.";
?>
</body> </html>
Summary & Conclusion

Conditional statements are mainly used to:


1. Take a decision based on a condition (comparison). For example,
decide whether a value is even or odd. (examples 1 to 5)
2. Handle input fields where the user selects one choice from several
options; Typically, radio buttons and drop-down lists. (examples 6
and 7)
3. Validate the correctness of data entered by the user. (example 8)

You might also like