PHP Control Structures If Else and Loop with Video in Khmer
Learn PHP control structures, if/else statements, and loops with practical examples and video tutorials in Khmer.
2026-08-18 ยท 5 min read
Hello my friends! Today we learn about PHP Control Structures like if/else, switch and loops (while, do while, for, foreach) in PHP! Very simple explanations with practical examples and video in Khmer. Let's go together!

1. Conditional Statement in PHP
Watch video about IF, ELSE, and Switch statement in PHP Khmer
What is If, Else in PHP?
if statement like decision maker. It check condition first. Check out the PHP Official Control Structures Manual for all syntax options. Make sure you understand Basic PHP Variables and Data Types before writing complex logic!
Example 1: Check number positive or not
<?php
$number = 101;
if ($number > 0) {
echo "Number is positive";
} else {
echo "Number is not positive";
}
?>
Example 2: Check student exam score pass or fail
<?php
$score = 75;
if ($score >= 50) {
echo "You Pass the exam! Congratulations!";
} else {
echo "You Fail the exam. Try again!";
}
?>
๐ก Tip: Always use double equals
==to check if equal, not single equal=! Single equal=is for set value to variable!
What is Switch Statement?
When you have many if else if else, code look messy. You can use switch statement! It look more simple and clean for check one variable with many values.
Example 1: Check color
<?php
$color = "red";
switch ($color) {
case "red":
echo "Color is red";
break;
case "blue":
echo "Color is blue";
break;
default:
echo "Color unknown";
}
?>
Example 2: Check day of the week
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start working day!";
break;
case "Friday":
echo "Weekend is coming!";
break;
default:
echo "Normal day";
}
?>
๐ก Tip: Don't forget
break;inside each case! If you forgetbreak;, PHP will continue run next case code even condition not match!
Watch video about IF ELSE vs Switch case in PHP Khmer
2. LOOPs in PHP
Loops help you repeat code again and again without copy paste many times. PHP have while, do while, for, and foreach.

Watch video about do while, while, for and foreach in PHP Khmer
while loop in PHP
while loop check condition first. If condition true, it run code. Then check again. If condition become false, loop stop!
Example 1: Count number 1 to 5
<?php
$x = 1;
while ($x <= 5) {
echo "Number: $x <br>";
$x++;
}
?>
Example 2: Countdown 5 to 1
<?php
$count = 5;
while ($count > 0) {
echo "Countdown: $count <br>";
$count--;
}
echo "Blast off!";
?>
๐ก Tip: Always update your loop variable like
$x++or$count--. If you forget, loop run forever (infinite loop) and crash server!
do while loop in PHP
do while run code one time first ALWAYS! After that, it check condition. If true, run again. If false, stop.
Example 1: Basic do while loop
<?php
$x = 6;
do {
echo "Number: $x <br>";
$x++;
} while ($x <= 5);
// Print "Number: 6" once even condition is false!
?>
Example 2: Simple pin input try
<?php
$attempt = 1;
do {
echo "Trying login attempt number: $attempt <br>";
$attempt++;
} while ($attempt <= 3);
?>
๐ก Tip: Use
do whilewhen you want your code to execute AT LEAST ONE TIME no matter what condition is!
for loop in PHP
When you know exactly how many times you want to repeat code, use for loop!
Example 1: Count 1 to 5
<?php
for ($x = 1; $x <= 5; $x++) {
echo "Number: $x <br>";
}
?>
Example 2: Print even numbers from 2 to 10
<?php
for ($i = 2; $i <= 10; $i += 2) {
echo "Even number: $i <br>";
}
?>
๐ก Tip:
forloop put initialization ($i=1), condition ($i<=5), and increment ($i++) all in one line. Very clean!
foreach loop in PHP
foreach loop is the best for array! It go through every item inside array one by one easily.
Example 1: Loop indexed array of colors
<?php
$colors = ["red", "blue", "green"];
foreach ($colors as $c) {
echo "Color: {$c} <br>";
}
?>
Example 2: Loop associative array (key => value)
<?php
$user = [
"name" => "Sokha",
"age" => 22,
"city" => "Phnom Penh"
];
foreach ($user as $key => $val) {
echo "$key : $val <br>";
}
?>
๐ก Tip: Use
$key => $valuesyntax inforeachwhen you need both array index/key name and its value! Read more on PHP Array and Data Types.
๐ก Next Lesson: Take your PHP skills further with PHP Functions with Video, or jump into database operations with PHP CRUD with MySQL Database!