0% found this document useful (0 votes)
35 views5 pages

Week 3 - Module 3

This document provides an overview of PHP conditional statements including if, if/else, if/elseif statements. It discusses the syntax and provides examples of using each type of conditional statement to evaluate conditions and execute different code blocks accordingly. Examples assess grades and provide remarks based on ranges of average scores. Learners will complete related activities and assessments to practice using PHP conditional statements.
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)
35 views5 pages

Week 3 - Module 3

This document provides an overview of PHP conditional statements including if, if/else, if/elseif statements. It discusses the syntax and provides examples of using each type of conditional statement to evaluate conditions and execute different code blocks accordingly. Examples assess grades and provide remarks based on ranges of average scores. Learners will complete related activities and assessments to practice using PHP conditional statements.
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/ 5

Week 3: Module 3

PHP CONDITIONAL STATEMENT


I. LEARNING OBJECTIVES

At the end of this lesson, the students should be able to:


1. Know the forms of conditional statement in PHP;
2. Know the list of relational operators;
3. Use conditions to change the value of a variable;
4. Use basic PHP function to trap errors and to get specific output;
5. Know the list of logical operators;
6. Create multiple branch of conditions alongside with HTML elements and logical operators combined; and
7. Create a simple PHP project with conditions.

II. CONTENT DISCUSSION OR ABSTRACTION

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.

PHP if() Statement


The if() is simple form of condition. It is composed of only one (1) branch or block of condition.

Syntax
If (condition) {
Code to be execute
}

Example 3.1
Filename: example3.1php

CS PC 212 Date Developed: August 5, 2022 Document No. 01


OBJECT ORIENTED PROGRAMMING Developed by: Revision # 00 Page | 1
Mary Grace M. Enriquez
<!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'];

if($grade < 75) {


$remark="Failing 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>

PHP if() else Statement

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'];

CS PC 212 Date Developed: August 5, 2022 Document No. 01


OBJECT ORIENTED PROGRAMMING Developed by: Revision # 00 Page | 2
Mary Grace M. Enriquez
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>

PHP if() elseif() Statement


This conditional statement can compose two (2) or more branch of condition but, only one (1) branch of condition will
be activated. If a branch of condition became "true" the other branch will automatically become "false”. An "else"
statement can also be added at the bottom.

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'];

$ave= ($pre+$mid+$end) /3;


if($ave < 75){
$remark="Failed";
}
Else if($ave >= 75 and $ave <=80) {

$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>

III. LEARNER’S EXPERIENCES OR ACTIVITIES/EXERCISES

*Please refer to the Posted activity on our Google Classroom

IV. ASSESSMENT/REFLECTION TOOLS

1. Class Participation
2. Quizzes
3. Activity Output

CS PC 212 Date Developed: August 5, 2022 Document No. 01


OBJECT ORIENTED PROGRAMMING Developed by: Revision # 00 Page | 5
Mary Grace M. Enriquez

You might also like