Hi all! today about PHP control structures with if, else, switch statement and loop (do while, while, for, and foreach) in PHP. It also cover PHP programming basics for new learners and friends who like PHP khmer. So, let's start.
If you want to learn more, please check our: [PHP Tutorial in Khmer Playlist].
PHP Variables and Data Type:Back 👈 👉 Next:Functions in PHP
Free Khmer Ebook Download (PDF): Database | Microsoft Access | Python Programming

If you want to learn more, please check our: [PHP Tutorial in Khmer Playlist].
1. Conditional statement in PHP
What is If, Else in PHP?
If statement is like decision maker, it check condition and if it true do something then Else, if condition false, you do other thing.
<?php
$number = 101;
if ($number > 0) {
echo "Number is positive";
} else {
echo "Number is not positive";
}
?>
What is Switch Statement?
When you have many conditions, you can use switch statement is more simple.
<php
$color = "red";
switch ($color) {
case "red":
echo "Color is red";
break;
case "blue":
echo "Color is blue";
break;
default:
echo "Color unknown";
}
?>
Video about IF, ELSE, and Switch statement in PHP in Khmer
Another video about IF ELSE vs Switch case in PHP Khmer
2. LOOPs in PHP
Loops consist of do while loop, while loop, for loop and foreach in php.
while loop in PHP
while loop check the condition first, and run code again and again when condition true. If condition false, loop stop.
<php
$x = 1;
while ($x <= 10) {
echo "Number: $x <br>";
$x++;
}
?>
do while in PHP
do while run code one first and again when condition true. If condition false, loop stop.
<php
$x = 6;
do {
echo "Number: $x <br>";
$x++;
} while ($x <= 5);
?>
for loop in PHP
for loop run code as many time then condition is true.
<php
for ($x = 1; $x <= 5; $x++) {
echo "Number: $x <br>";
}
?>
Video about do while, while, for and foreach statement in PHP in Khmer
foreach in PHP
foreach loop best for array. It loop every item in array.
<php
$colors = ["red", "blue", "green"];
foreach ($colors as $c) {
echo "Color: {$c} <br>";
}
?>
PHP Variables and Data Type:Back 👈 👉 Next:Functions in PHP
Free Khmer Ebook Download (PDF): Database | Microsoft Access | Python Programming