dan forys

blog

Learning PHP - Part 2: variable basics

Posted on 2009-10-25 - Comments

After part 1, you should be familiar with the echo statement, which outputs a string to the browser:

echo 'This is a string';

(Note that from now on the opening (<?php) and closing (?>) PHP tags are omitted from examples, but should still be included in any source code you create)

On its own, it does not appear to be useful; after all we could have just written 'This is a string' in HTML for the same effect. The real power comes from using PHP variables.

Think of a variable as a named box, in which you can place something. Later, you can retrieve the thing you put in the box, you just look it up by its name. Assigning values to variables

Creating a variable is as easy as giving it a name and telling it what value you want it to store. In PHP, variables always begin with a dollar sign ($)

A variable name must begin with a letter, or an underscore. The rest of the name can be any combination of letters, numbers and/or underscores.

$string = 'This is a string';

This creates a new variable with the name of '$string' and stores the value 'This is a string' inside it. You can combine the echo statement with a variable to output the contents of a variable to the browser:

$string = 'This is a string';
echo $string;

This will output 'This is a string' as with the first example.

Any subsequent values you set will overwrite the previously stored value:

$string = 'This is a string';
$string = 'This overwrites the previous value';
echo $string; // outputs 'This overwrites the previous value'

Here we’ve introduced a style of PHP comment. Comments are totally ignored by PHP making them useful for documenting your code. I’ve used the ‘//’ comment, where PHP will ignore everything up to the end of the line.

Variable types

In PHP, variables can be one of several types. These types affect what operations you can perform on the variable and how the values behave. Three basic variable types are listed below.

Strings

Strings are collections of characters. You denote strings by enclosing them in single or double quotes:

$string1 = 'This is a single quoted string';
$string2 = "This is a double quoted string";

There are some differences with how single and double quoted strings are interpreted – we’ll talk about that later.

You can add (concatenate in PHP-speak) two strings together using a period ‘.’:

$string = 'This string ' . 'is concatenated from two parts';
echo $string; // outputs 'This string is concatenated from two parts';

You can also do this with two variables containing strings:

$string1 = 'This string ';
$string2 = 'is concatenated from two parts';
echo $string1 . $string2 // same result as above
Integers

Integers are whole numbers. A whole number is one without a decimal point.

$integer = 42; 
echo $integer; // outputs 42

You can perform mathematical operations on integers:

$integer1 = 50;
$integer2 = 20;
echo $integer1 - $integer2; // outputs 30
echo $integer1 + $integer2; // outputs 70
Floating point numbers

Floating point numbers are numbers with a decimal point. In practice, they work similarly to integers:

$float1 = 10.5;
$float2 = 0.2;
echo $float1 - $float2; // outputs 10.3
echo $float1 + $float2; // outputs 10.7

Note that floating point numbers have a very important gotcha:

Inside the computer memory, floating point numbers are not stored as exact representations. This means rounding errors can occur. For this reason, never use floating point numbers where accuracy is important; such as in financial transactions for example.

Other types

There are several other types of variable in PHP. The other types are slightly more advanced and will be introduced later as we need them.