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
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.
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 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 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.
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.
- Next generation console wishlist 2013-02-18
- Digital conversations preserved 2011-02-21
- Pastel de Nata recipe 2011-01-10
- Fixing a bricked D-Link DSL-G624T 2010-01-22
- Learning PHP - Part 6: functions 2009-11-03
- Learning PHP - Part 5: your first dynamic web page 2009-11-03
- Learning PHP - Part 4: controlling flow 2009-11-02
- Symptoms of a Wordpress hack 2009-11-02
- Learning PHP - Part 3: array basics 2009-10-26
- Learning PHP - Part 2: variable basics 2009-10-25
- Learning PHP - Part 1: introduction 2009-10-24
- Unsetting HTTP headers in PHP 2008-08-06
- Intermittent 1px gap in Firefox 3 2008-07-30
- Understanding Linux file permissions 2008-07-29
- Step by step: Moving code between Subversion repositories 2008-07-23
- Novell client on OpenSuse 10.3 2007-10-08
- Removing Windows from Apple's Bootcamp 2007-10-01
- HTTP authentication in PHP 2007-06-12
- Microformats and me 2007-06-11