Php Code– Codemeets – 04
php code 4. In this lesson, we will focus on control statements in PHP, including the IF statement, Switch statement, and loops. Finally, we will create a simple Grading Calculator to apply these concepts effectively.
Control Statements
Control statements are used to determine which statements are executed based on specific conditions. They decide if a certain action should take place or how many times an action should be repeated.
There are three main types of control statements:
- IF
- Switch
- Loops
IF Statement in PHP
The IF statement checks a condition or multiple conditions. It is used to decide whether a block of code should be executed. One of the key features of the IF statement is its ability to evaluate different conditions. The following example will help clarify its use.
<?php
$name = "apple";
if ($name="apple"){
echo "true";
}
else{
echo "false";
}
?>
This Code output is true.
Switch Statement in PHP
The Switch statement is used to evaluate multiple conditions, making it faster and more efficient than using multiple if-else statements. It checks the value of a variable and executes the corresponding case block.
<?php
$ grade = $_POST["grade"];
switch ($grade) {
case 'A':
echo "A";
break;
case 'B':
echo "B";
break;
case 'C':
echo "C";
break;
case 'S':
echo "S";
break;
default:
# code...
break;
}
Loops in PHP
There are four main types of loops in PHP:
- For loop
- While loop
- Do-While loop
- Foreach loop
Here, I will demonstrate the first three loops using PHP code. All of these loops will produce the same output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.
For Loop
The For loop is simple and easy to use. It is used when you know in advance how many times you want to repeat a block of code.
<?php
for ($int = 0; $int <= 10; $int++) {
echo "$int <br>";
}
?>
While Loop
<?php
$int = 1;
while($int <= 10) {
echo "$int <br>";
$int++;
}
?>
Do while Loop
<?php
$int = 1;
do {
echo "$int <br>";
$int++;
} while ($int <= 10);
?>
Foreach Loop
This is a special loop for php. This is commonly used for arrays. see below example
<?php
$laptops = array("Dell", "Apple", "Hp", "Azus");
foreach ($laptops as $value) {
echo "$value <br>";
}
?>
Include
you can see below example its do include html and php file.
<?php
include "header.html"
include "index.php"
?>
Simple Grading Calculator
Here’s an example of a simple Grading Calculator that uses the knowledge we’ve learned so far, such as if statements and HTML forms. This calculator takes the user input for marks and displays the corresponding grade based on the marks.
<!DOCTYPE html>
<html>
<head>
<title>Grading Calculator</title>
</head>
<body>
<h2>Grading Calculator</h2><br>
<form action="site.php" method="POST">
Marcks : <input type="number" name="num">
<input type="submit">
<br>
Grade is :
<!--php code-->
<?php
$num = $_POST["num"];
if($num>=75){
echo "A";
}elseif ($num>=65) {
echo "B";
}elseif ($num>=65) {
echo "C";
}elseif ($num>=65) {
echo "S";
}else{
echo "F";
}
?>
</form>
</body>
</html>
Save this file. file name is site.php.
Thank for all! do you have any question Click here!
See you Next Lesson Soon!