PHP Functions with Video in Khmer
Master PHP functions with step-by-step Khmer video tutorials and practical code examples. Learn how to declare custom functions, pass parameters, set default values, and use return statements effectively.
2026-08-18 ยท 4 min read
Hi my friends! Today we learn about PHP Functions. This blog post explain PHP functions in Khmer! I will explain how you create and use function in PHP as simple as possible. Let's start!
1. What is a PHP Function?
PHP function is a block of code. You write code once and can use again and again! Read the official PHP Functions Manual for in-depth documentation. If you need to review loops first, check out PHP Control Structures If Else and Loop.
Why we use PHP Functions?
- Reusability: Write code 1 time, call many times!
- Clean Code: Make code organized and easy to read.
- Easy Maintenance: If need fix bug, just change 1 place inside function!
How to Create & Call Basic Function
You make function by keyword function, give it a name, and add {} curly braces for code inside.
Example 1: Simple greeting function
<?php
function sayHello() {
echo "Hello friends, learn PHP go to - code.rean.me <br>";
}
// Call function to run code
sayHello();
?>
Example 2: Show website footer note
<?php
function showFooter() {
echo "Copyright 2026 - Rean code in Khmer";
}
showFooter();
?>
๐ก Tip: Function names in PHP are NOT case-sensitive (
sayhello()andsayHello()call same function), but always usecamelCasefor clean code!
2. Functions with Parameters
Parameters like variable input data inside function. You pass data when calling function!
Example 1: Function with 1 parameter
<?php
function greeting($name) {
echo "Hello " . $name . "! Welcome to PHP lesson.<br>";
}
greeting("Sokha");
greeting("Bopha");
?>
Example 2: Function with multiple parameters (Calculate price)
<?php
function calculatePrice($item, $price, $quantity) {
$total = $price * $quantity;
echo "Item: $item | Total: \$$total <br>";
}
calculatePrice("Book", 5, 3);
calculatePrice("Pen", 1, 10);
?>
๐ก Tip: Separate multiple parameters inside
()with comma,. Make sure you pass arguments in SAME ORDER as parameters!
3. Function with Default Parameter Value
If user not pass value into parameter, PHP will use default value you set!
Example 1: Welcome user with default name
<?php
function welcomeUser($name = "Guest") {
echo "Welcome, " . $name . "!<br>";
}
welcomeUser("Dara"); // Print: Welcome, Dara!
welcomeUser(); // Print: Welcome, Guest!
?>
Example 2: Set default theme mode
<?php
function setTheme($theme = "light") {
echo "Current web theme is: " . $theme . "<br>";
}
setTheme("dark");
setTheme(); // Default light
?>
๐ก Tip: Always put parameters with default values at the END of parameter list! Example:
function demo($a, $b = 10).
4. Functions with Return Value (return)
Instead of just echo, function can calculate result and send value back using return keyword!
Example 1: Sum 2 numbers and return result
<?php
function addNumbers($a, $b) {
return $a + $b;
}
$sum = addNumbers(10, 20);
echo "Result is: " . $sum; // Output: 30
?>
Example 2: Check if number is even
<?php
function isEven($number) {
if ($number % 2 == 0) {
return "Even Number";
} else {
return "Odd Number";
}
}
echo isEven(8); // Output: Even Number
?>
๐ก Tip:
returnstatement immediately STOPS function execution! Any code written belowreturninside function will never run!
5. Video Tutorial in Khmer
Watch full video explanation about PHP Functions in Khmer:
๐ก Next Lesson: Connect your PHP functions to a database in PHP CRUD (Create, Read, Update, Delete) with MySQL Database!
Hope you learn something new today! Happy Coding! Sharing is caring!