home

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:

  1. variable declaration
  2. variable call

STEP 1. Variable declaration:

To declare a variable you have to follow this syntax:

$varName = value;

PLEASE NOTE:

STEP 2. Variable usage:

To refer to the value of a variable, just write its name.

<?php #variables.php
//variable declaration, storing a STRING (a piece of text)

$myName = "john";
//variable declaration, storing a NUMBER
$myAge = 40;

//variables usage
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.
 */

?>

PLEASE NOTE:

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:

You can use isset() to determine whether a variable has been set and is not null.

<?php #variables.php
//variable declaration
    $myVar = "some value";

    if( isset($myVar) )
    {
         echo $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>
<p>like this:"EOD"
EOD;

echo $text;

/*
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 <<<,

like this:"EOD"
*/

?>

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>&pound;$price</strong> costs
<strong>£$pricePlusVat</strong> including VAT.</p>";
/*
An item costing £20 costs £24 including VAT.
*/

?>

Click here to read about data types.


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Copyright © 2014 by Cramerz.com.