Week 3 - Module 3
Week 3 - Module 3
Conditional statement is one of the most important control structure in programming. Conditional statement is used in
to perform different actions and output based on the given inputs. Like other programming languages PHP also have
its conditional statement. This chapter will give you knowledge and understanding about PHP conditional statement.
PHP conditional statement:
• if()statement
• if() else statement
• if() else if()statement
• switch statement
PHP conditional statement used Comparison operator to compare values as part conditional statement.
Syntax
If (condition) {
Code to be execute
}
Example 3.1
Filename: example3.1php
$grade="";
$remark="?";
if(isset($_POST ['remark'] )){
$grade=$_POST ['grade'];
}
?>
<p>
Final Grade: <input type="text" name="grade" size="8" value="<?=$grade;?>"/>
<input type="submit" name="remark" value="Remark"/>
</p>
<p>Remark: <?=$remark;?></p>
</form>
</body>
</html>
An if() else condition statement is composed of one branch of condition with “else” at the bottom which will activate if
the above statement became false
Syntax:
If(condition) {
Code to be executed
}
Else {
Code to be execute
}
EXAMPLE 3.2
Filename: example3.2php
<!doctype html>
<html>
<head>
<title>PHP Sample 2.6</title>
</head>
<body>
<form method="POST" action="">
<?php
$grade="";
$remark="?";
if(isset($_POST ['remark'] )){
$grade=$_POST ['grade'];
<p>Remark: <?=$remark;?></p>
</form>
</body>
</html>
Syntax:
If (condition) {
Code to be execute
}
Elseif(condition) {
Code to be execute
}
Elseif(condition) {
Code to be execute
}
.
.
.
Else{
Code to be execute
}
Example 3.3
Filename: example3.3php
<!doctype html>
<html>
<head>
<title>PHP Sample 3.3</title>
</head>
<body>
<form method="POST" action="">
<?php
$grade=0;
$remark="?";
if(isset($_POST['remark'] )){
$grade=$_POST ['grade'];
If(!is_numeric ('grade')) {
$remark="Invalid Input!";
}
CS PC 212 Date Developed: August 5, 2022 Document No. 01
OBJECT ORIENTED PROGRAMMING Developed by: Revision # 00 Page | 3
Mary Grace M. Enriquez
Else if($grade < 75) {
$remark="Failing Grade!";
}
Else {
$remark="Passing Grade!";
}
}
?>
<p>
Final Grade: <input type="text" name="grade" size="8" value="<?=$grade;?>"/>
<input type="submit" name="remark" value="Remark"/>
</p>
<p>Remark: <?=$remark;?></p>
</form>
</body>
</html>
An “!is_numeric()” function is added in the condition to check if the entry is not a number.
Example 3.4
Filename: Example3.4php
<!doctype html>
<html>
<head>
<title>PHP Example 3.4</title>
</head>
<body>
<form method="POST" action="">
<h2>PHP Example 3.4</h2>
<?php
$ave=0;
$remark=$pre=$mid=$end="";
if(isset($_POST ['compute'])){
$pre=$_POST ['prelim'];
$mid=$_POST['midterm'];
$end=$_POST['endterm'];
$remark="Below Average";
}
else if($ave > 80 and $ave <= 85){
$remark="Average" ;
}
else if($ave > 85 and $ave <= 90){
$remark="Above Average";
}
else if($ave > 90){
$remark="Excellent";
}
}
?>
<p>Prelim Grade: <input type="text" name="prelim" value="<?=$pre;?>"></p>
<p>Midterm Grade: <input type="text" name="midterm" value="<?=$mid;?>"></p>
<p>Endterm Grade: <input type="text" name="endterm" value="<?=$end;?>"></p>
<p><input type="submit" name="compute" value="Compute"></p>
<p>
Average Grade: <?=number_format ($ave, 2) ;?><br/>
Remark: <?=$remark; ?><br/>
CS PC 212 Date Developed: August 5, 2022 Document No. 01
OBJECT ORIENTED PROGRAMMING Developed by: Revision # 00 Page | 4
Mary Grace M. Enriquez
</p>
</form>
</body>
</html>
1. Class Participation
2. Quizzes
3. Activity Output