Basics Lesson 4 of 17
PHP Variables and Data Types
Create PHP variables with $, learn the data types (string, int, float, bool, array, null), check them with var_dump and gettype, and convert between types.
In PHP, every variable name starts with a dollar sign $. You don't declare a type; PHP figures it out from the value.
Creating variables
<?php
$name = "Maria Clara";
$gradeLevel = 10;
$average = 94.5;
$isOfficer = true;
echo "$name is in Grade $gradeLevel with an average of $average.";
Output
Maria Clara is in Grade 10 with an average of 94.5.
- Names start with
$, then a letter or underscore:$total,$_count. - They can't start with a number:
$1stis invalid. - They're case-sensitive:
$Nameand$nameare different. - Common styles are camelCase (
$gradeLevel) or snake_case ($grade_level). Pick one and be consistent.
Data types
| Type | Example |
|---|---|
| string | "Rizal", 'Grade 9' |
| int | 42, -7 |
| float | 89.5, 0.12 |
| bool | true, false |
| array | ["Math", "Science"] |
| null | null (no value) |
Inspecting a variable: var_dump
<?php
var_dump("Rizal");
var_dump(42);
var_dump(89.5);
var_dump(true);
var_dump(null);
var_dump([1, 2]);
Output
string(5) "Rizal"
int(42)
float(89.5)
bool(true)
NULL
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}var_dump shows the type and the value, which makes it the best debugging tool for beginners. gettype($x) returns just the type name, and print_r($array) prints arrays in a shorter form.
Changing values
<?php
$baon = 150;
$baon -= 45; // lunch
$baon -= 20; // fare
$baon += 50; // from Lola
echo "Baon left: ₱$baon";
Output
Baon left: ₱135
Type juggling
PHP converts types automatically when needed:
<?php
var_dump("5" + 3);
var_dump("5" . 3);
var_dump(10 / 4);
var_dump(10 / 5);
var_dump(intdiv(10, 4));
Output
int(8) string(2) "53" float(2.5) int(2) int(2)
Because PHP uses . to join strings and + only for math, "5" + 3 is 8 (not "53" like in JavaScript).
Explicit conversion
<?php
$qty = (int) "12";
$price = (float) "8.50";
$text = (string) 2026;
echo $qty * $price, " | ", $text . "!";
Output
102 | 2026!
Constants
<?php
const PASSING_GRADE = 75;
define("SCHOOL", "Mabini NHS");
echo SCHOOL . " passing grade: " . PASSING_GRADE;
Output
Mabini NHS passing grade: 75
Constants have no $ and can't be changed. Write them in UPPERCASE.
Checking if a variable has a value
<?php
$nickname = null;
var_dump(isset($nickname));
var_dump(empty(""));
echo $nickname ?? "Guest";
Output
bool(false) bool(true) Guest
isset is true if the variable exists and isn't null. empty is true for "", 0, null, false and empty arrays. ?? gives a fallback value.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04