Php Code– Codemeets – 04
php code 4. I will focus on Getting Control Statements (IF statement, Switch, loops). Finally, Create a simple Grading Calculator. It can provide extensive knowledge.
Control Statements
A control statement is a statement that determines whether other statements are active. If a statement decides whether another statement should be executed, or one of the two statements to be executed. A loop determines the number of times another expression should be executed.
Basically have 3 control statement
- IF
- Switch
- Loops
IF Statement in php
If a statement is used to check the condition, it can be a condition or more. The special feature of if is that you can check any gap. The following example will make it clear to you.
<?php
$name = "apple";
if ($name="apple"){
echo "true";
}
else{
echo "false";
}
?>
This Code output is true.
Switch
The switch expression is used to check several non-gap conditions. This works very fast and gets the idea below php code.
<?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
Loops have main 3 loops in php
- For loop
- While loop
- Do While loop
- Foreach Loop
Now I show First 3 loops using Code. See Below all code output is same. output is : 1,2,3,4,5,6,7,8,9,10
For Loop
this is simple and easy loop in any programming languages.
<?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
See the example below and give it a try. This is a simple hierarchical calculator code. It has an HTML code that you can try on your computer. This calculator is made using what we have learned so far. So you can get a very good experience by practicing this.
<!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!