dev.rean.me
php

Basic PHP Variables and Data Types Explained with Examples & Video Demo

Learn PHP variables and data types in a simple way with clear examples, rules, pro tips, and video demo in Khmer.

2026-08-17 · 5 min read

Share:

Hello my friends! Today, let learn together about PHP Variables and Data Types step by step with video demo in Khmer that you can follow along with me.

Yeah, let get started!

Image for What is PHP Variables and Data Types?

Watch Video Tutorial (Khmer)

Watch video first to see how PHP variables and data types work in code editor:

1. What is a PHP Variable?

A variable in PHP is like a container box to store information (text, numbers, prices, or user status). Read more on the official PHP Variables Manual. If you haven't set up your local web server yet, check out How to Download Install and Configure XAMPP first!

In PHP, variable name must always start with a dollar sign ($)!

<?php
$myVar = "Hello World!";
$age = 25;
$price = 19.99;

?>

Important Rules for PHP Variable Names:

  1. Must start with $, followed by letter or underscore _ (e.g. $name, $_user).
  2. Cannot start with a number! ($1name ❌ WRONG -> $name1 ✅ CORRECT).
  3. Case-Sensitive: $age and $AGE are 2 different variables!
  4. Cannot contain spaces ($first_name or $firstName ✅).

2. Main PHP Data Types

Data Type is the type of data you put inside your variable box. Here are the main data types in PHP:

1. String (Text Data)

String is text written inside single quotes ' ' or double quotes " ".

$greet = "Hello, PHP in Khmer!";
$name = 'Dara';

2. Integer (Whole Number)

Integer is number without decimal point (can be positive or negative).

$age = 25;
$score = 100;
$temperature = -5;

3. Float or Double (Decimal Number)

Float is a number with decimal point.

$price = 101.99;
$gpa = 3.75;

4. Boolean (True or False)

Boolean represents 2 states: true (1) or false (0). Super useful for if/else condition!

$isValid = true;
$isWorking = false;

5. Array (Multiple Values)

Array lets you store multiple items inside one single variable! For a deep dive into array types, see PHP Array and Data Types with Examples.

// Array with color items
$colors = array("Red", "Blue", "Green");

// Short array syntax []
$fruits = ["Apple", "Banana", "Mango"];

6. NULL (Empty Value)

A variable of data type NULL means it has no value assigned to it.

$userRole = null;

3. How to Check & Print Variables in PHP?

<?php
$name = "Sok";
echo "My name is " . $name; // Output: My name is Sok
?>

Debug variable data type with var_dump():

var_dump() shows you both data type and value of variable:

<?php
$price = 10.5;
var_dump($price); // Output: float(10.5)
?>

4. Pro Tips for Beginners

💡 Tip 1: Double Quotes vs Single Quotes Inside double quotes " ", PHP automatically reads variables!

$name = "Dara";
echo "Hello $name"; // Output: Hello Dara ✅
echo 'Hello $name'; // Output: Hello $name ❌ (literal string)

💡 Tip 2: PHP is Loosely Typed Language You don't need to declare data type manually (like int $age or string $name). PHP automatically detects the data type based on the value you assign!

💡 Tip 3: Always use $isWorking = false; (don't forget $!) In PHP, writing is_working = false; without $ sign causes a Syntax Error. Always put $ before variable names!

5. Complete Practical PHP Example (Copy & Paste to Test)

Here is a complete practical example combining all variables and data types together. Save this file as index.php inside C:\xampp\htdocs\demo\ and open http://localhost/demo/:

<?php
// 1. Declare variables with different data types
$studentName = "Dara";              // String
$age = 22;                         // Integer
$gpa = 3.85;                       // Float
$isEnrolled = true;                // Boolean
$skills = ["PHP", "HTML", "MySQL"]; // Array
$middleName = null;                // NULL

// 2. Display information using echo
echo "<h2>Student Profile Card</h2>";
echo "<p><strong>Name:</strong> " . $studentName . "</p>";
echo "<p><strong>Age:</strong> $age years old</p>";
echo "<p><strong>GPA:</strong> $gpa / 4.0</p>";
echo "<p><strong>Status:</strong> " . ($isEnrolled ? "Active Student ✅" : "Inactive ❌") . "</p>";

// 3. Display array skills
echo "<p><strong>Skills:</strong></p>";
echo "<ul>";
foreach ($skills as $skill) {
    echo "<li>$skill</li>";
}
echo "</ul>";

// 4. Debug variable data types
echo "<hr>";
echo "<h4>Debug Data Types (var_dump):</h4>";
echo "<pre>";
var_dump($studentName); // string(4) "Dara"
var_dump($age);         // int(22)
var_dump($gpa);         // float(3.85)
var_dump($isEnrolled);  // bool(true)
var_dump($skills);      // array(3)
var_dump($middleName);  // NULL
echo "</pre>";
?>

💡 Next Lesson: Continue your learning with PHP Control Structures If Else and Loop to master conditional logic!

Happy coding my friends! Try writing these variables in your VS Code and test them in browser!

← Back to php
Share: