Php Variables Tutorial
A variable is a storage area. You can think about variables like temporary boxes in a computer's memory to store data. PHP uses Variable to store different type of Data.
For example, if we want to store our name in a PHP script and then see it on the web page, we can use a PHP variable to store our name.
You need two steps to work with variables:
- variable declaration
- variable call
STEP 1. Variable declaration:
To declare a variable you have to follow this syntax:
$varName = value;
- variables in PHP start with a dollar sign ($)
- variables' names are case sensitive
- you can use alphanumeric characters and underscores (no spaces)
- the first character of a string name can be a letter or an underscore (no numbers)
- you can assign a value to a variable using the assignment operator (=)
- you cannot name a variable $this
- if the value is a piece of text (a string) you have to wrap the value in between quotation marks
- if the name of the variable contains more than one word, please use lower camel case notation (e.g. $thisIsMyName or $varNameHere)
STEP 2. Variable usage:
To refer to the value of a variable, just write its name.
//variable declaration, storing a STRING (a piece of text)
$myName = "john";
//variable declaration, storing a NUMBER
$myAge = 40;
echo "<p>Hello, there. My name is $myName and I am $myAge years old.</p>"
Hello, there. My name is john and I am 40 years old.
- If you echo a variable name between double quotation marks ("$myNAme") you will output the value of that variable (e.g. john)
- If you echo a variable name between single quotation marks ('$myNAme') you will output the name of that variable (e.g. $myName)
Variables declaration and datatype
Unlike other programming languages, PHP is a loosely typed language. For this reason you do not have to declare a type while declaring a new variable. While variables do not have specific types, PHP still maintains the concept of a type.
The principal types in PHP are:
- Integer (e.g. 4)
- Float (e.g. 2.3)
- String (e.g. "dog")
- Boolean (true or false)
You can use isset() to determine whether a variable has been set and is not null.
$myVar = "some value";
if( isset($myVar) )
The above example declares a variable $myVar, then displays it only if it has been correctly set.
Click here to read more about IF statements.
Long multiline strings can be stored into variables using HEREDOC syntax:
<?php #variables.php $name = 'joe';
// HEREDOC syntax:
$text = <<<EOD
<p>This string can contain both "double" and </p>
<p>'single' quotes, and any variables such as $name will be interpolated</p>
<p>(i.e replaced with their value). The string ends at the first occurence</p>
<p>of the sequence of characters specified at the beginning after <<<,</p>
This string can contain both "double" and
'single' quotes, and any variables such as joe will be interpolated
(i.e replaced with their value). The string ends at the first occurence
of the sequence of characters specified at the beginning after <<<,
You can concatenate strings using the . operator:
<?php #variables.php $firstString = 'Monkeys '; $secondString = 'like '; $thirdString = 'bananas';
//The . operator adds strings together (concatenation) $fourthString = $firstString . $secondString . $thirdString; echo "<p>$fourthString</p>";
Monkeys like bananas
Of course you can use variables to store and operate with Numbers:
<?php #variables.php //storing numbers in variables $vatRate = 1.2;
$price = 20;
//operating $pricePlusVat = $price * $vatRate; echo "<p>An item costing <strong>£$price</strong> costs
<strong>£$pricePlusVat</strong> including VAT.</p>";
An item costing £20 costs £24 including VAT.
Click here to read about data types.