Variables
By the end of this lesson you'll store text, numbers, and true/false values in $variables , tell PHP's types apart with var_dump , and sidestep the == pitfalls that trip up almost every beginner.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Creating Variables
A variable is a named place to keep a value so you can use it later. In PHP a variable name always begins with a $ , followed by a letter or underscore, then letters, numbers, or underscores — for example $name or $first_name . You create one by assigning a value with a single = , which means "store this in the box" — not "is equal to". Names are case-sensitive , so $Name and $name are two different boxes.
Notice the last two lines: assigning to $age again simply replaces what was inside. A variable holds one value at a time — the most recent thing you put in it.
2️⃣ PHP's Data Types
The type of a value is what kind of thing it is. PHP has eight types in total — string (text), int (whole numbers), float (decimals), bool ( true / false ), null (no value), plus array , object , and callable for later lessons. You'll use the first five constantly. The single most useful tool for learning them is var_dump() , which prints both the type and the value.
Two things to file away: var_dump shows a string's length ( string(9) means 9 characters), and PHP confusingly calls a float a "double" when you ask gettype() . That's a historical quirk — double and float mean the same thing in PHP.
3️⃣ Dynamic, Loose Typing
Unlike Java or C#, PHP is dynamically typed : you never write the type when you create a variable, and PHP works it out from the value. The same variable can even change type later — put text in it now, a number in it next. This is called loose typing , and it's flexible and fast to write, but it's also the reason for the comparison surprises you'll meet in section 5.
4️⃣ Quotes, Interpolation & Concatenation
How you quote a string changes what PHP does with it. Double quotes ( "..." ) interpolate — PHP swaps any $variable inside for its value and processes escapes like \n . Single quotes ( '...' ) are literal — you get the characters exactly as typed, $name and all. To stick strings together, use the dot operator ( . ), which is PHP's concatenation operator (not + ).
Now you try. Fill in each ___ using the 👉 hint, then run it and check against the Output panel.
One more — this time about quoting and joining strings.
5️⃣ Type Juggling: == vs ===
Type juggling is PHP automatically converting types during an operation — "5" + 3 gives 8 because the string is turned into a number. It also happens during loose comparison with == , which converts both sides to a common type first. That's why 5 == "5" is true . The fix is strict comparison with === , which checks value and type, so 5 === "5" is false . Use === by default and only use == when you genuinely want juggling.
PHP 8 cleaned up the worst of the old traps (for example 0 == "hello" used to be true and is now false ), but loose comparison can still surprise you with empty values and null . Reaching for === removes the guesswork entirely.
6️⃣ Constants
A constant is a value that's fixed once and can never change — perfect for things like a tax rate or a maximum limit. Constants have no $ and are written UPPER_CASE by convention. Define one with const (the modern, preferred way) or the older define() function. Trying to reassign a constant is a fatal error — which is exactly the safety you want for values that must never drift.
7️⃣ Heredoc & Nowdoc
For multi-line text — an email body, a block of HTML — quotes get awkward. Heredoc ( LABEL ) is like double quotes spread over many lines: it interpolates your variables. Nowdoc ( 'LABEL' , with the label in single quotes) is like single quotes: everything is literal . In both, the closing label must sit at the start of its own line.
📋 Quick Reference — Variables & Types
No code is filled in this time — just a brief and an outline. Write it yourself, run it on onecompiler.com/php or your own machine, then check your result against the expected output in the comments. This is exactly the write-run-check loop you'll use on every real script.
Practice quiz
How must every variable name begin in PHP?
- With an @ sign
- With the word var
- With a $ sign
- With a capital letter
Answer: With a $ sign. Every PHP variable name starts with a dollar sign, e.g. $name.
What does the single = operator do in PHP?
- Stores (assigns) the right value into the left variable
- Compares two values
- Tests for equality
- Joins two strings
Answer: Stores (assigns) the right value into the left variable. A single = is assignment: it stores the value on the right into the variable on the left.
Which of these is NOT one of PHP's main everyday types?
- string
- int
- float
- decimal
Answer: decimal. The everyday types are string, int, float, bool, and null. PHP has no type called 'decimal'.
What does var_export(5 === "5") output?
- true
- false
- 1
- 0
Answer: false. === is strict: an int and a string differ in type, so 5 === "5" is false.
What does var_dump(5 == "5") show?
- bool(true)
- bool(false)
- int(5)
- string(1)
Answer: bool(true). Loose == juggles the string "5" to the int 5, so the comparison is true.
Which quotes make echo print the value of a variable rather than its literal name?
- Single quotes '...'
- Backticks
- Double quotes "..."
- No quotes at all
Answer: Double quotes "...". Double quotes interpolate variables; single quotes are literal.
Which operator joins (concatenates) two strings in PHP?
- The + operator
- The . (dot) operator
- The & operator
- The , comma
Answer: The . (dot) operator. PHP concatenates strings with the dot operator, not +.
What does gettype() return for a float value like 4.5?
- float
- decimal
- number
- double
Answer: double. By a historical quirk, gettype() reports a float as 'double'.
Which is true about a constant defined with const?
- It must start with $
- It cannot be changed once set
- It can be reassigned later
- It is always an integer
Answer: It cannot be changed once set. Constants have no $ and cannot be reassigned; trying to is a fatal error.
What does HEREDOC (<<<LABEL) behave like?
- Single quotes (literal)
- A comment
- Double quotes (interpolates variables)
- An array
Answer: Double quotes (interpolates variables). Heredoc acts like double quotes and interpolates variables; nowdoc acts like single quotes.