Variables in PHP

Topics

  1. Important characteristics of variables in PHP
  2. Difference between double quotes and single quotes
  3. Variables without value
  4. For different types of variables
    1. Scope of Variables
    2. Private variables
    3. Global variables
    4. Static variables
  5. Parameters / Arguments
  6. Variable naming rules
  7. Variable variables

PHP tutorials

Important characteristics of variables in PHP

  • PHP doesn’t have any particular syntax or command for declaring variables.
  • A variable in PHP is always prefixed by “$”
  • A variable is declared when a value is first assigned to it:
    For example:

    $mystring = ‘Hello World!’;
  • Since you do not need to declare a variable explicitely in PHP before using it, therefore PHP is called a loosely typed language.
  • Moreover, you also do not need to define any data type for the variable being used. PHP automatically assigns the suitable data type to the variable, depending on the variable value.

Difference between double quotes and single quotes

Please note that there is a difference between the double quotes “” and the single quotes ‘’ while declaring / using variables in PHP.
Though both can be used anywhere, but the general rules are:

  • Double quotes are used when you want the string inside the quotes to be parsed.
    Here, parsing means that if there are any more variables in the string
  • Single quotes are used when you want to print the string as it is and not parse it.

Example1:

$name = ‘John’;
echo “My name is $name”;

It will print: My name is John

Example2:

$name = ‘John’;
echo ‘My name is $name’;

It will print: My name is $name

Variables without value

If you want to create a variable without assigning it a value, then you assign it the value of null.

For different types of variables

  • For string variables (i.e. characters), quotes (either double or single) are mandatory

    Example:

    $name = ‘John’; or $name = “John”;
  • For integer variables (i.e. numbers), no quotes are required

    Example:

    $x = 20;

Scope of variables in PHP

  • Local
  • Global
  • Static
  • Parameters

Local Variables

When a variable is declared within a function in PHP, it has a local scope i.e. it can only be accessed within that function and not outside.
Such a variable is said to have ‘local scope’.
Example:

function mynewfunc()
{
	$a = 5;
}
echo $a;

The above function will not print any value, since the variable $a is defined inside the function ‘mynewfunc’ and is thus not accessible outside this function.

It will throw an error like this:
“Notice: Undefined variable: a in C:\xampp\htdocs\exp.php on line 11”
Such variables are deleted as the function gets over, therefore they are unknown outside their function.

Global Variables

Such variables are defined outside i.e. they are not defined inside any function, and are thus said to have global scope.
Example:

$a = 5;
function mynewfunc()
	{
		$a = 5;
        }
echo $a;

The above function will print the following value: 5
Since the variable $a is also defined outside the function ‘mynewfunc’ and is thus accessible outside this function. Therefore, Global variables can be accessed from any part of the script that doesn’t lie inside a function.
NOTE: If you want to access a global variable inside the function, then you need to prefix the keyword ‘global’ where the variable is being called.
Example:

$a = 5;
function mynewfunc()
	{
		global $a;
		$b = $a + 8;
		echo $b;
        }

The above function will print the following value: 13
NOTE: PHP also has a universal variable $GLOBALS which contains all the variables declared outside the functions. So, any variable can be accessed inside a function by writing $GLOBALS[‘variable name’].
Example:

$a = 5;
function mynewfunc()
	{
		$b = $GLOBALS[‘a’] + 8;
		echo $b;
        }

The above function will print the following value: 13

Static Variables

When you want to access the local variables even after their function is completed, then you need to declare those variables with the keyword ‘static’.
As normally the variables are deleted on function completion.
Then, each time the function is called, the variable retains its value from the last time the function was called. Though the variable still remains local to the function.
Example:

static $name;

Parameters / Arguments

These are not exactly variable scopes, but are used for passing values to functions. These are part of the function declaration only.
Example:

function mynewfunc ($var1,$var2,...)
{
   // function code
}

Here, the variables $var1 and $var2 are passed as parameters.

Variable naming rules

Mandatory rules

  • Variable names in PHP must start with a letter or underscore ‘_’
  • A variable can only contain numbers, characters and underscore

Optional rules

  • Variables with more than one word can be separated with underscores.
    Eg. $my_var
  • Variables with more than one word can also be distinguished with capitalization.
    Eg. $myVar

Variable variables in PHP

  • It is meant for the purpose when you need to change the name of the variables frequently.
  • For a variable name which can be set and used dynamically.
  • A variable variable takes the value of a variable and treats it as the name of a variable.

Example1:

$a = ‘Hi’;
$$a = ‘Hello’;

So, here the php variable tree will have the following two variables:
$a and $hi

It is similar to using pointers in C.

For more info on its usage, visit the following link:
More info on variable variables in PHP

Example2:

$a = 'hello';
$b = "a";
$c = "b";
echo $c;
echo $$c;
echo $$$c;

The above function will print the following value: bahello
o Generally used when doing object oriented programming in PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *