Home > PHP, PHP Tutorials > Tutorial 2 – PHP Variables

Tutorial 2 – PHP Variables

In the earlier PHP tutorial we saw an introduction to what PHP is and how it is used in web programming. Click here to read that Post

In this tutorial on PHP Variables we will discuss the following:

  • What is a variable?
  • Variables in PHP
  • PHP is a loosely typed language
  • Variable naming rules in PHP
  • Use of the double dollar ($$) variables

What is a variable?

A variable is the name given to a memory allocation to store or retrieve information. A variable can be used to store values like strings (text), numbers, array or objects. Once a variable is set it can be used anywhere in your script.

Variables in PHP

In PHP a variable is declared using the dollar ($) symbol. The dollar symbol instructions the PHP language that you are interested in creating a variable. Look at the example below:

<?php

$a = “SUNIL”;
echo $a;

?>

In the above example you will see that I have defined a variable by the name $a. $a is nothing but a name given to the memory allocation where ‘SUNIL’ is stored. Before a variable is created a space in the memory is allocated to that variable and then that space is associated with the variable name. This is called memory allocation.

Look at the diagram below to understand the internals of a memory and how it is assigned:

variable.gif

In the above example the series of blocks that you see is memory. The numbers 100, 101, 102, etc are numbers assigned to each memory block which is 1 byte each. As per the example above, when you assign the value “SUNIL” to $a this is how the internals look like.




PHP is a loosely typed language

Unlike languages like C, Java & C#; PHP is a loosely typed language. This means that you don’t have to declare the variable before using it and you don’t need to specify the data type of the variable.

Data type of the variable means that type of data that can be stored in the variable. In languages like ‘C’ you cannot declare a variable before specifying its data type. The data type could either be character, integer or floating point numbers. This means that if a variable is defined as a number you cannot assign a string to it.

You can see from the example above that I did not have to define the type of a variable. I just had to create a variable and assign memory to it like $a = “SUNIL”;

This means that in PHP, variables are declared automatically as you use it.

Variable naming rules in PHP

  • A variable name must start with a letter or an underscore “_”
  • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)




Use of double dollar ($$) sign in PHP

Double dollar is a powerful way to programmatically create variables and assign values them.

E.g:

<?php

$a = “amount”;
$$a =1000;
echo $amount; //echo’s 1000 on screen

?>

In the example above, you can see that the variable $a stores the value “amount”. The moment you use a double dollar sign ($$) you are indirectly referencing to the value of $a i.e. amount.

So, with this like $$a = 1000; the variable $amount gets created and I assign the value 1000 to $amount. This way you can programmatically create variables and assign values to them.

In the next post I will be talking more on PHP Strings. Subscribe below to receive updates on the next tutorial as it gets published.

Previous Posts

Introduction to PHP Web Programming
PHP 5 Tutorials – Introduction

Categories: PHP, PHP Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. matthew
    March 4th, 2008 at 20:24 | #1

    If PHP variable are not assigned types when declared, how does the computer know how much memory to assign that variable. The variable could be of type char or of type String

    Matt

  2. admin
    March 5th, 2008 at 09:07 | #2

    Hi Matthew,

    The variable system with PHP is that of ‘variable’ type. The variable changes its type depending on the type of data that you assign to it. Its much like the ‘var’ type in JavaScript.

    Sunil

  3. August 14th, 2009 at 08:18 | #3

    Good Tutorials. Very useful

  4. Khai
    September 2nd, 2009 at 08:33 | #4

    Useful Tutorial indeed, been programming PHP for 3 years and looking at this tutorial makes me have a clearer mindset of internal flows while programming. ^.^

  1. October 31st, 2007 at 07:02 | #1
Enter this code to leave comment (Sorry, but bots get me crazy :) )