Variables are used for storing values, such as a text string “hello” or a numeric value “7”. They form the basis for any PHP code.
Creating a Variable
$ followed by
$my_variable = 'hello world';
- Must be alphanumeric and can contain underscores
(a-z A-Z 0-9 _).
- Must start with an underscore or a letter.
- Spaces aren’t allowed in variables
- Variables with more than one word should be separated with underscores such as
- Another way to separate multiple worded variables is through capitalisation such as
Assigning a Value
There are a few ways to set variable values within PHP, including two different types of quotation marks.
My recommendation is to use a single quotation mark.
'value' will be treated as a non-executable string. Whereas
"value" will check if there’s anything to execute during processing. This will impact speed.
If you want to pass a variable’s value to another variable I recommend the following.
$var_1 = 'Hello'; $var_2 = $var_1 . ' world'; // Fastest method
Numeric strings are very easy to understand and to use. They behave in a similar way to double quotation strings and you don't need to do much different. Things to remember -
BODMAS(brackets, operation, division, multiplication, addition, subtraction)
Basic Numeric String
Let’s start with the most basic numeric string we can. We want to add two values together
1 + 2 = 3, although it’s very rare that you’ll use two static numbers like this.
$string = 1 + 2; echo $string; // 3
Numeric with a variable
Now lets make numeric strings slightly more complicated by using a variable. This is the most practical application of numeric variable calculations.
$i = 5; $string = $i + 2; echo $string; // 7
Special Numeric Strings
What about saving time on simple calculations? Well PHP has you covered. For example, you may wish to add 1 every time you go through a loop.
$i = 11; // Define the variable $i++; // Add 1 to $i $i--; // Subtract 1 from $i $i += 2; // Add 2 to $i $i -= 2; // Subtract 2 from $i
Seeing a Variable
There are a few different ways to display variables on a PHP page. There are two correct ways, and one that may confuse you. The first and most common method using the
echo function. The second function you’ll see is the
echo 'hello'; // hello print 'hello'; // hello $var = 'hello world'; echo $var; // hello world print $var; // hello world
What’s the difference between echo and print?
On the face of it
echo returns an empty value, whereas
This becomes a problem when using PHP functions to check on the success of another function. If you use
echo the function will think it’s failed, but a success message will be displayed.
Ultimately this is bad practise and you should instead be using the
return function in this case. The problem with using return is that it doesn’t display the text, but rather passes it between functions. Don’t worry if this isn’t making much sense yet!
Showing an Error Message
Like many things in PHP there are two ways to kill your script and display an error message. The most common method is using the
die function, but you can also use
die 'hello'; // hello print 'test'; // doesn't run exit 'hello'; // hello echo 'test'; //doesn't run
What’s the difference between exit and die?
There is no difference between
die() in PHP.
Subscribe for Updates
Get notified about my latest content first and receive invitations for subscriber only competitions.