PHP Lessons - Lesson 2 - Variables in PHP
PHP variables are used to store values such as strings, numbers, or arrays. Once a variable is declared, it can be reused throughout your script. All variables in PHP begin with the $ symbol.
Variables are one of the core concepts in programming. To understand programming, you must have a clear understanding of what a variable is, how and where it is stored, and what happens to it during code execution. A variable is a value that can change during program execution.
Variables are pieces of data processed in a program that have a name. As you know, data is stored and processed in the computer's memory—operating in RAM while the program is running and saved to persistent storage when needed. Variables can be of different types: numbers, characters, text, booleans, procedures, dates, etc., and each can have subtypes (e.g., integers, floats). Depending on the type, a program will allocate a specific amount of memory. These memory slots are given the variable’s name and will store the variable's value either until the program ends or the value is overwritten. The variable name stays constant, but the value can change. In PHP, both the value and the type of a variable can change at runtime.
The correct way to declare a variable in PHP:
$var_name = value;
The equals sign =
is the assignment operator, which tells PHP to assign the value on the right to the variable on the left.
New PHP developers often forget to add the $ at the start of the variable. PHP will throw an error in such a case.
Try this PHP code in an
index.php
file, intentionally omitting the dollar sign:<?php var_name = 'Hello, World!'; ?>
When you visit the site (e.g., http://test), you’ll see an error message similar to:
When declaring a string variable, wrap the value in either single or double quotes. Just make sure to match the opening and closing quote type. Numeric variables should not be in quotes. When printing a string variable, do not wrap the variable name in quotes.
Let’s create one string variable and one numeric variable. Replace the code in index.php
with:
<?php
$txt = "Hello World!";
$x = 16;
print $txt;
echo $x;
?>
Visiting the site again will output:
Hello World!16
In this example, both print
and echo
can be used to output variables — the result will be the same. PHP outputs both string and numeric variables in a single line.
Another feature of PHP is that you do not need to explicitly declare a variable. You can immediately assign a value to it:
$variable = 'New variable';
A variable can even change its type dynamically:
<?php
$var1 = '1 new variable';
$var2 = 6;
$var3 = $var1 + $var2;
print $var3;
?>
The result of mixing a string and a number like this is:
7
Surprising, isn’t it?
You can also reassign variables to change their values:
<?php
$var1 = 14;
$var1 = $var1 + 1;
print $var1;
?>
This results in:
15
This works by evaluating the current value of the variable and assigning the result back into itself (14 + 1).
And yes, you can (though it’s not recommended) use non-Latin characters in variable names:
<?php
$переменная1 = 'Hello, World!';
print $переменная1;
?>
Which gives:
Hello, World!
Not bad, right? Maybe PHP should be taught in schools — in Russian!
Of course, this is just the beginning. There are more variable types: arrays, objects, resources, booleans, and more. We’ll get there soon.
Summary of this lesson
In PHP, a variable is automatically created the moment you assign a value to it. Naming rules for variables:
- Variable names must start with a letter or underscore
_
. - Variable names may only contain letters, numbers, and underscores (A-Z, a-z, 0-9, and _).
- Variable names cannot contain spaces. If the name contains multiple words, use underscores (
$my_string
) or camelCase ($myString
).
PHP is a loosely typed language. This means you do not need to declare variable types before assigning values. In strongly typed languages, you must define both the type and name before use. PHP, however, automatically converts a variable to the correct type depending on its value.