If you have no programming experience then you may be unfamiliar with the concept of variables. A variable is a value, stored in the computer's memory, which can be varied.
To be able to read a variable's value or alter its value we don't need to know where it's stored in memory - PHP takes care of that - we just need to know its name.
Variable Names
There are naming conventions that must be followed. Variable names ...
Must be preceded with a dollar sign or they won't be recognized e.g. $my_variable.
Must start with a letter or an underscore e.g. $_my_variable.
Must contain only alpha-numeric or underscore characters i.e. a-z, A-Z, 0-9 or _.
Are case sensitive e.g. $My_Variable and $my_variable are different variables.
Variable names are usually chosen to reflect their use and if two words make things clearer then underscores or capitalization is normally used e.g. $customer_count or $CustomerCount.
Variable Types
PHP supports four scalar types of variable and two compound types. A scalar variable can only hold one value at a time whereas a compound variable can hold more than one value (compound variables are covered in a later tutorial).
Scalar Variable Types
Boolean - that can only hold one of two values either true or false e.g. $test = true.
Integer - that can hold whole numbers e.g. $number = 6.
Float - that can hold floating point numbers e.g. $number = 6.23.
String - that can hold a string of characters e.g. $name = "Dave".
Using Variables
With many scripting languages a variable's name and type must be declared before it can be used. PHP does not require this and although it's convenient, it can lead to hard-to-find bugs.
For example, you may have created a string variable called $convenient and at some point in the script you wish to print out the value of this variable.
When using echo($convenient) you make a typing error and use echo($conveinent) instead. The script will treat the typing error as a new empty variable and print out nothing.
This wouldn't happen with languages that require a variable to be declared since the script would stop with an "undeclared variable" error.
Variable Assignment
In PHP variables are created and a value assigned in one statement. The assignment of the value implicitly defines the variable's type i.e. integer, float, string etc.
In the examples above the $var_1 and $var_2 assignments implicitly define the variables as integer type. With $var_3 the assignment implicitly defines the variable as a float.
Operating on Variables
It's common to want to add or subtract 1 to or from a variable (increment or decrement) and this can be achieved in different ways.
Examples of Variable Operations
<?php
// Different ways to increment $var_1
$var_1 = $var_1 + 1;
$var_1 += 1;
$var_1 ++;
// Different ways to decrement $var_1
$var_1 = $var_1 - 1;
$var_1 -= 1;
$var_1 --;
?>
<?php
// Different ways to double $var_1
$var_1 = $var_1 * 2;
$var_1 *= 2;
// Different ways to halve $var_1
$var_1 = $var_1 / 2;
$var_1 /= 2;
?>
Single and Multi-line Comments
<?php
// A single line comment
/* An example of a multi-line
comment that stretches
over more than one line */
?>
Multi-line comments are useful when debugging scripts as they allow you to comment out whole sections of code rather than delete them.
String Variables
A string variable consists of one or more characters e.g. $my_string = "Hello there";.
Strings can be added together (concatenated) using the . character.
Examples of Concatenation
<?php
$my_string = "The beginning" . " and the end";
Constants, as the name suggests, are values that don't change. A constant is used to set a constant parameter that can be accessed from any point in the script (for example the maximum number of names or addresses to display on one page).
Names for constants follow the same rules as for variables but are not preceded with a $ sign and by convention are in upper case.