Variables & Data Types
PHP variables start with $. Learn strings, integers, floats, and booleans. This is a foundational concept in server-side web development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world PHP experience. Take your time with each section and practice the examples
25 min•By Priygop Team•Last updated: Feb 2026
PHP Variables
In PHP, all variables start with a dollar sign ($). PHP is loosely typed — you don't need to declare the type. Variable names are case-sensitive ($name and $Name are different). PHP automatically determines the type from the value assigned.
PHP Data Types
- string — Text: $name = 'Alice'; or $name = "Alice";
- int — Whole numbers: $age = 25;
- float — Decimal numbers: $price = 9.99;
- bool — True or false: $isStudent = true;
- array — List of values: $fruits = ['Apple', 'Banana'];
- null — No value: $x = null;
- gettype() — Check the type of a variable
Variables Example
Example
<?php
// PHP variables always start with $
$name = "Alice";
$age = 25;
$height = 5.6;
$isStudent = true;
// echo prints output
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Height: " . $height . "<br>";
echo "Student: " . ($isStudent ? "Yes" : "No") . "<br>";
// Check types
echo gettype($name) . "<br>"; // string
echo gettype($age) . "<br>"; // integer
echo gettype($height) . "<br>"; // double (float)
echo gettype($isStudent) . "<br>"; // boolean
// String interpolation (double quotes)
echo "Hello, $name! You are $age years old.<br>";
?>Try It Yourself: PHP Variables
Try It Yourself: PHP VariablesHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|38 lines|1171 chars|✓ Valid syntax
UTF-8