Introduction
A variable holds a value, and every value has a type — what kind of thing it is. PHP has four types beginners use every day: string (text), int (whole numbers), float (numbers with a decimal point) and bool (true or false). There are a few more, but these four carry almost every program.
PHP decides the type from the value, not from a declaration. Write $age = 20; and PHP knows it is an integer. Write $name = "Ali"; and it is a string. That freedom is handy — and it is also where the classic beginner bug lives: "5" + 2 is 7, because PHP quietly turns the text "5" into the number 5 when you add. Helpful, until it surprises you.
Operators are the symbols that do things to values: + - * / % for maths, . for joining text, = for storing, and comparison operators for asking questions. The one to tattoo on your memory is the dot: in PHP, . joins strings. In JavaScript the same job is done by +, which is why learners coming from JavaScript keep writing "Ali" + "!" and getting 0.
(Roman Urdu: Type matlab value ki kisam — text, poora number, decimal, ya sach/jhoot. . text jorta hai, + number jama karta hai — ye farq yaad rakhna.)
Key points
- The four everyday types:
string(text in quotes),int(whole numbers),float(decimals),bool(true/false). var_dump($x)prints a value AND its type. It is the fastest way to see what PHP thinks a value is.gettype($x)returns the type name as text, like"integer"or"string".- Maths operators:
+add,-subtract,*multiply,/divide,%remainder,**power. .joins strings."Ali" . "!"givesAli!. This is the concatenation operator.+on strings tries to add numbers."5" + 2is7;"Ali" + 2is2with a warning, because "Ali" becomes 0.- Comparison operators:
==(equal value),===(equal value AND type),!=(not equal),><>=<=. ==vs===matters.5 == "5"is true (PHP converts);5 === "5"is false (different types).- Assignment shortcuts exist:
$x += 5means$x = $x + 5;$name .= "!"appends to a string. is_string(),is_int(),is_float(),is_bool()ask "is this value this type?" and answer true or false.
Examples
Example 1 — Seeing the four types with var_dump
<?php
$name = "Ali";
$age = 20;
$height = 5.9;
$isStudent = true;
var_dump($name);
var_dump($age);
var_dump($height);
var_dump($isStudent);
?>What you will get:
string(3) "Ali"
int(20)
float(5.9)
bool(true)var_dump shows the type, and for a string also its length in characters. Keep it in your pocket — it settles every "what type is this?" argument.
Example 2 — The dot joins, the plus adds
<?php
echo "Ali" . " " . "Ahmed";
echo "<br>";
echo 5 + 2;
echo "<br>";
echo "5" + 2;
echo "<br>";
echo "5" . 2;
?>What you will get:
Ali Ahmed
7
7
52"5" + 2 adds numbers (7), but "5" . 2 joins text ("52"). Same values, different operator, different answer. When a result looks wrong, check the operator first.
Example 3 — Remainder and power
<?php
echo 10 % 3;
echo "<br>";
echo 2 ** 3;
echo "<br>";
echo 7 / 2;
?>What you will get:
1
8
3.5% gives the remainder after division (10 ÷ 3 leaves 1). ** is power (2 × 2 × 2). Division of integers can give a float — 7 / 2 is 3.5, not 3.
Example 4 — == forgives the type, === does not
<?php
var_dump(5 == "5");
var_dump(5 === "5");
var_dump(5 != "6");
?>What you will get:
bool(true)
bool(false)
bool(true)== only checks the value, so 5 equals "5". === checks value AND type, so the integer 5 is not the string "5". In real code, prefer === — it catches the type surprises before your users do.
Example 5 — Assignment shortcuts
<?php
$cups = 0;
$cups += 3;
echo $cups;
echo "<br>";
$greeting = "Hello";
$greeting .= ", Ali!";
echo $greeting;
?>What you will get:
3
Hello, Ali!+= adds and stores in one step. .= joins and stores — the string version of the same idea.
Quick recap
- Four main types: string, int, float, bool;
var_dumpshows value and type. .joins text;+adds numbers — never mix them up.%is the remainder;**is power;/can give a decimal.==compares values;===compares value AND type — prefer===.$x += 5and$str .= "!"are shortcuts for changing a value in place.
Common mistakes
- Joining text with
+—"Hello" + $namegives 0, not a greeting. Fix: use.for text. - Adding with
.—"5" . "2"gives "52", not 7. Fix: use+for numbers. - Using
==when the type matters —0 == "Ali"can behave unexpectedly. Fix: use===unless you truly want conversion. - Trusting a form value's type — form input always arrives as a string, so
"90" + 10works but"90" === 90is false. Fix: cast it first,(int) $_POST["marks"]. - Forgetting that
/gives a float —7 / 2is 3.5, and code expecting 3 breaks. Fix: useintdiv(7, 2)for a whole-number answer, or round it. - Reading
var_dumpoutput as an error — it is a debugging tool, not a crash. Fix: removevar_dumplines before the page goes live.
Practice
- Make four variables (one of each type) and
var_dumpall four — write down each type. - Print
"5" + 2and"5" . 2on one page and write down why the answers differ. - Work out 17 % 5 and 3 ** 4 by hand first, then check with PHP.
- Test
var_dump(10 == "10");andvar_dump(10 === "10");— write down the difference in one line. - Start
$total = 100;, then use+=to add 50 and.=on a string to build "Total: 150" — print both. - Use
is_int()on 20 and on "20" and print both answers — explain the difference in one line. - Store
$a = "12"; $b = 3;and print$a + $b,$a . $b, and$a * $b— predict each answer before you run it.
Practice quiz
Practice questions — try them, then check your answers in the interactive quiz on the full lesson page.
1. What does the dot . do in PHP?
- It adds two numbers
- It joins two strings together
- It ends a statement
- It starts a comment
2. What is "5" + 2 in PHP?
- "52"
- 7, because + adds numbers and PHP turns "5" into 5
- An error
- 3
3. What is the difference between == and ===?
- They are the same
- == checks the value only; === checks the value AND the type
- === checks the value only; == checks value and type
- == is for text, === is for numbers
The interactive quiz with instant green/red answers lives on the full lesson page.