PHP Variables

variable

Concept of Variable

Sometimes in your program, you need to store data. During the execution of the program, computer processor stores those data in its memory cells before using those. If you use 100 different data in your program, then processor allocates 100 or more memory cells to store those data. Every memory cell has a number. In your program, you may need to use same data many times, therefore, to use a data for the next time in the program, you need to know the address of memory cell(s) where it is stored. But when you work with lots of data, it is not easy to remember those cell numbers. And, with the increase of data, it becomes harder or about impossible. To remember those cell numbers easily, programmers give each cell an easy name. This name is called variable. For example, if your age is 25 and it is stored in memory cell number 6927, is it not easy to remember the variable “age” instead of 6927 to refer the 25? You don’t need to know where 25 is stored in memory; variable is used to reference that location number.

Variable in memory

When you run a program, a data might be stored in any memory cell address, but, next time you run it, that data might be stored in different cell address. That means, data in a particular cell address varies. This is how the name “variable” came.

Variable declaration

Before using a variable, you need to declare it.

Syntax
The syntax for declaring a variable would be-

$variable_name;

Example:
The syntax for declaring a variable named today is-

<?php
$today;
?>

Variable naming rules

A PHP variable’s name can be anything as long as it follows the rules below-

  1. Variable name must be starts with dollar ($) sign. ex. $today, $height etc.
  2. First character must be a letter or underscore. Ex. $today, $_today etc.
  3. Other than first character, there may have any number letters (A-Z,a-z), numbers (0-9), or underscores(_). ex. $_4site etc.
  4. Variable name is case-sensitive. That means $today and $Today are 2 different variables.
  5. Any variable that doesn’t fulfill the above rules is an invalid variable.

  1. Variable name should be meaningful so that it tells what it is for. Ex. $username.
  2. Predefined variables names in PHP start with a underscore then only capital letters, ex. $_REQUEST, therefore, avoid declaring your variables in this format.

You can’t use $this as a variable name. It is a reserved word.

Assigning values to variable

What is the use if a variable doesn’t hold a value? There are 2 ways to assign a value to a variable-

  1. By value
  2. By reference
  1. By value

    Syntax
    The syntax for assigning a value to a variable is-

    $variable_name = value;

    Here, ‘variable_name’ is the name of the variable and ‘value’ is any type of data(ex. Integer, string etc.). The above line can be read as “assign ‘value’ to the variable ‘variable_name’”.

    Example:
    To declare a variable named today and assigning a value of Wednesday to it, you could write-

    <?php
    $today=”Wednesday”;
    ?>

    It’s a very good practice to assign a value to the variable when declaring it.

    AS PHP interpreter handles data before it becomes HTML, therefore, it is called PHP:Hypertext Preprocessor.

    Alert

    It’s a common tendency for beginners to think the above example as “$day is equal to Wednesday” when they see the = sign. It could be true in algebra, but not in PHP programming.
  2. By reference

    Syntax
    The syntax for assigning a reference to a variable is-

    $variable_name = &$existing_variable_name;

    Here, ‘variable_name’ is the name of the variable and ‘existing_variable_name’ is a previously declared variable that will be used as reference to the ‘variable_name’ variable. The above line can be read as “assign ‘existing_variable_name’ as the reference to the variable ‘variable_name’”.

    Example:
    To declare a variable named tomorrow and to assigning a reference of day variable to it, you could write-

    <?php
    $today=”Wednesday”;
    $tomorrow=&$today;
    ?>

    As the new variable is referencing the existing variable, so their values are same. Therefore, changing value to a variable will also change the value in other one.

Displaying variables

Syntax
The syntax for displaying a variable is-

echo $variable_name;
or
print $variable_name;

Example:
The syntax for displaying a variable named today is-

<?php
$today=”Wednesday”;
echo $today;
print $today;
?>

Output:
Line 3 and 4 both display Wednesday.

Variable scopes

Variable scope refers the parts of a script where the variable’s value is available after it is declared within a script. To understand scope properly, function knowledge is required. You‘ll about variable scope in the next lesson after function.

Variable types

There are two types of variables in PHP-

  1. User-defined variables
  2. Predefined variables

User-defined variables

As the names implies these variables are defined in the script by the users. In the above examples the variables we defined are all user-defined variables.

Example

<?php
$today=”Friday”;
?>

In the above example, the variable $today that I created is a user-defined variable.

Predefined variables

  1. PHP comes with a large number of predefined variables to any script that it runs.
  2. Predefined variables play an important role in PHP programming. They provide lots of useful information such as server information, information that comes after submitting a form etc.
  3. As these variables are structures as associative arrays and you’re not introduced array yet, we’ll discuss about predefined variables after array.
‹‹ PHP Basic Syntax : Previous Lesson Next Lesson: PHP Data Type››