Skip to main content
Web Development Tutorials

PHP

Variables

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.

Define

Creating a variable and assigning it a value.

Display

Show variable values on the page.

Calculate

Use variables to add, subtract, and multiply values.

Exit

Show a variable when killing a page

Define

Creating a Variable

Creating a variable is as easy as 1, 2, 3, especially if you’re used to working with JavaScript. Simply prefix an alphanumeric word with a $ followed by = 'value';.

[php]
$my_variable = ‘hello world’;
[/php]

Naming Conventions
  • 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 $my_var
  • Another way to separate multiple worded variables is through capitalisation such as $myVar
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.

[php]
$var_1 = ‘Hello’;
$var_2 = $var_1 . ‘ world’; // Fastest method
[/php]

Calculate

Numeric Strings

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)
  • + Add
  • - Subtract
  • * Multiply
  • / Divide
  • () Brackets
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.

[php]
$string = 1 + 2;

echo $string; // 3
[/php]

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.

[php]
$i = 5;
$string = $i + 2;

echo $string; // 7
[/php]

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.

[php]
$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
[/php]

Display

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 print function.

[php]
echo ‘hello’; // hello
print ‘hello’; // hello

$var = ‘hello world’;
echo $var; // hello world
print $var; // hello world
[/php]

What’s the difference between echo and print?

On the face of it echo and print do exactly the same thing, display text. But there is one minor difference. echo returns an empty value, whereas print returns 1.

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!

Exit

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 exit.

[php]
die ‘hello’; // hello
print ‘test’; // doesn’t run

exit ‘hello’; // hello
echo ‘test’; //doesn’t run
[/php]

What’s the difference between exit and die?

There is no difference between exit() and die() in PHP.

Subscribe for Updates

Get notified about my latest content first and receive invitations for subscriber only competitions.

PHP Basics

Coming Soon