A variable stores a value so you can use it later. In PHP, every variable name starts with a dollar sign: $name, $price, $isLoggedIn. That $ makes variables easy to spot, even in the middle of HTML.
PHP works out a variable's type from the value you give it, and a variable can hold a different type later. The main types are strings for text, integers and floats for numbers, and booleans for true or false.
Because PHP spends most of its time producing text, it has two handy ways to build strings: joining pieces with the dot operator, and dropping variables straight into double-quoted strings. In this lesson you'll use both.
Creating variables
$name = "Amir";
$age = 17;
$age = $age + 1; // now 18- Start with
$, followed by a letter or underscore. Letters, digits and underscores can follow. - Names are case-sensitive:
$scoreand$Scoreare different variables. =stores a value; assigning again replaces it.
Popular naming styles are camelCase ($firstName) and snake_case ($first_name). Pick one and use it consistently.
Data types
| Type | Example | Notes |
|---|---|---|
| string | "Hello", 'Hi' | Text, in single or double quotes |
| int | 42, -7 | Whole numbers |
| float | 3.14, 0.5 | Numbers with a decimal point |
| bool | true, false | Yes or no |
| null | null | No value at all |
To inspect a variable while learning, use var_dump($x). It shows both the type and the value, such as int(18) or string(4) "Amir".
Joining strings with .
PHP joins strings with a dot, not a plus sign:
$first = "Ada";
$last = "Lovelace";
$full = $first . " " . $last; // "Ada Lovelace"
$full .= "!"; // add to the endNumbers are converted to text automatically when you join them: "Age: " . 18 gives "Age: 18".
Single quotes, double quotes and interpolation
- Double quotes replace variables with their values. This is called interpolation:
"Hi $name"becomesHi Amir. Use curly braces when the variable touches other letters or is an array item:"{$count}th visit". - Single quotes treat everything literally:
'Hi $name'stays exactlyHi $name.
Double quotes also understand escape sequences such as \n for a new line. Single quotes are simpler and are a good choice for text with no variables in it.
Constants
A constant holds a value that never changes while the script runs. Constants have no $ sign and are written in capitals by convention:
const VAT_RATE = 0.2;
define('SITE_NAME', 'Codevra');
echo SITE_NAME; // Codevra
echo 100 * VAT_RATE; // 20const is the usual choice at the top of a file or inside a class. define() is a function, so it can create a constant conditionally or with a name built at run time. Trying to change a constant is an error, which is exactly the point: settings such as tax rates and limits can't be overwritten by accident. The constants lesson covers both forms in detail.
Checking and converting types
Because a variable's type can change, PHP gives you tools to check and convert it:
| Tool | Example | Result |
|---|---|---|
gettype($x) | gettype(1.5) | "double" |
get_debug_type($x) | get_debug_type(1.5) | "float" |
is_int, is_string, is_float, is_bool | is_int(5) | true |
is_numeric($x) | is_numeric("12.5") | true |
| Casts | (int) "42", (string) 7, (float) "2.5" | 42, "7", 2.5 |
Text from forms and URLs always arrives as a string, even when it looks like a number, so check it with is_numeric or a filter before calculating. PHP 8 is stricter than older versions: "abc" + 1 throws a TypeError, and "12abc" + 1 gives 13 with a warning.
PHP Variables Syntax
Variables and strings
$variable = value;
$text = "Hello, $name"; // interpolation
$text = 'Hello, ' . $name; // concatenation
$text .= " and welcome"; // append
var_dump($variable); // type and valuePHP Variables Summary
- Variables start with
$and are case-sensitive. - Main types: string, int, float, bool and null.
var_dump()shows type and value. - Join strings with
.; add to a string with.=. - Double quotes interpolate variables; single quotes don't.
- Built-in functions like
strlen()andstrtoupper()work with text.
PHP Variables Examples
Each example shows the code, the output it produces and what each part does.
A profile with interpolation
<?php
$name = "Amir";
$age = 17;
$city = "Cairo";
echo "Name: $name\n";
echo "Age next year: " . ($age + 1) . "\n";
echo "{$name} lives in {$city}.\n";
echo 'Single quotes: $name' . "\n";Name: Amir
Age next year: 18
Amir lives in Cairo.
Single quotes: $nameLine-by-line explanation
- Line 6Double quotes put the value of $name into the text.
- Line 7The dot joins strings; brackets make the maths happen first.
- Line 8Curly braces mark exactly where each variable starts and ends.
- Line 9Single quotes print the dollar sign and name literally.
Types with var_dump
<?php
$title = "Codevra";
$visits = 120;
$rating = 4.8;
$isOpen = true;
$discount = null;
var_dump($title, $visits, $rating, $isOpen, $discount);string(7) "Codevra"
int(120)
float(4.8)
bool(true)
NULLLine-by-line explanation
- Line 8var_dump shows each value's type, and for strings, their length.
String functions
<?php
$raw = " hello world ";
$clean = trim($raw);
echo strlen($clean), "\n";
echo strtoupper($clean), "\n";
echo ucfirst($clean), "\n";
echo str_replace("world", "PHP", $clean), "\n";11
HELLO WORLD
Hello world
hello PHPLine-by-line explanation
- Line 3trim() removes the spaces at both ends.
- Line 5strlen() counts the characters, including the space in the middle.
- Line 7ucfirst() capitalises only the first letter.
- Line 8str_replace() swaps one piece of text for another.
Common PHP Variables Mistakes
Forgetting the dollar sign
name = "Amir";$name = "Amir";Without $, PHP thinks name is a constant and stops with a syntax error.
Joining with +
echo "Total: " + 5;echo "Total: " . 5;+ is only for maths. In PHP 8, "Total: " + 5 throws an error because the text isn't a number.
Expecting single quotes to interpolate
echo 'Hello $name'; // Hello $nameecho "Hello $name"; // Hello AmirPHP Variables in Real Projects
Strings in web pages
Almost everything a PHP site sends is text: page titles like "$productName | My Shop", greetings like "Welcome back, $firstName", and email bodies. Cleaning user input with trim() and changing case with strtolower() before saving an email address are everyday tasks.
PHP Variables FAQ
Why do PHP variables start with a dollar sign?
The $ marks a name as a variable, which lets PHP put variables straight into double-quoted strings, as in "Hello $name", and makes them easy to spot among HTML. The style came from Perl and shell scripting, which strongly influenced early PHP.
Do I need to declare a variable before using it in PHP?
No. A variable is created the first time you assign to it. Reading a variable that was never assigned gives a "Undefined variable" warning and the value null, so always assign a starting value, such as $total = 0;, before using it.
What is the difference between null, an empty string and 0?
null means "no value at all", "" is a string with no characters, and 0 is the number zero. They are all treated as false in an if, but they aren't identical: null === "" is false. Use === or functions such as is_null when the difference matters.
Can a PHP variable change its type?
Yes. $x = 5; followed by $x = "five"; is allowed. This flexibility is convenient but can hide mistakes, so modern PHP code adds type declarations to function parameters and return values, and many projects enable declare(strict_types=1); for stricter checking.
How do I see what is inside a variable?
Use var_dump($x) for the type and value, or print_r($array) for a readable view of an array. For quick debugging in a web page, wrap the output in <pre> tags so it keeps its line breaks. Remove debugging output before publishing a page.
PHP Variables Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the code so it prints Hi Lea.
name = "Lea";
echo "Hi " $name;
What does this print?
$item = "tea";
echo "I like $item";
What does this print?
$a = "5";
$b = "3";
echo $a . $b;
Which function returns the number of characters in a string?
What does this print?
$item = "pen";
echo 'Buy a $item' . " and a $item";
PHP Variables Quiz
5 questions to check what you learned.
What must every PHP variable name start with?
- @
- #
- var
- $
What does
var_dump(3.0)output?- int(3)
- float(3)
- string(3)
- 3.0
Which operator joins two strings in PHP?
- .
- +
- &
- ,
What does
echo "Score: {$points}pts";print if $points is 40?- Score: 40pts
- Score: {$points}pts
- Score: $pointspts
- An error
Which operator joins two strings in PHP?
- +
- .
- &
- ,