PHP Tutorial: PHP Variables

PHP Variables

Variable is a symbol or name that stores a value. Variables are used for storing values such as numeric values, characters, character strings, or memory addresses so that they can be used in any part of the program.

All variables in PHP starts with dollor ($) symbol like $name.

A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.

Example:

$salary = 10000;

$name = ‘James’;

First one is the interger variable and second one is the string variable. String variables used single or double quotes(‘’, “”) for a value.

 

Variable naming conventions

There are a few rules for declaring a variable name:

  • Variables must start with a letter or underscore "_".
  • Variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • It is good practice to seprate variable name with underscores(_), if the vaariables with more than one word. or
  • Variables with more than one word can also be distinguished with capitalization. $myVariable

Example:

<?php

$abc = 'Welcome';  //valid 

$Abc = 'W3resource.com'; //valid 

$9xyz = 'Hello world';  //invalid; starts with a number 

$_xyz = 'Hello world';  //valid; starts with an underscore 

$_9xyz = 'Hello world';  //valid 

$aäa = 'Hello world';  //valid; 'ä' is (Extended) ASCII 228. 

?> 

Note: In language such as C, C++ you have to declare the name and type of the variable before use it. In PHP the type of the variable does not need to be declared before use it, because types are associated with values rather than variables. As a result a variable can change the type of its value as much as we want.

In the following example none of the variables are declared before they are used, fact is $height is floating number and $width is an integer.

<?php

$height = 5.6;

$width = 9.5; 

$area=$height*$width; 

echo "Area of the rectangle is : $area"; 

?> 

Assigning by Reference

This means that the new variable simply points the original variable. Changes to the new variable affect the original, and vice a versa.

Example:

<?php

$a='books'; 

$b=&$a; 

$b="my $b"; 

echo $b; 

echo '
'; 

echo $a; 

?> 

Output:

my bob

my bob

Scope of Variable:

Variables can be declared anywhere in the script. There are two types of scope,

  • Local scope: here varibales are declared and accessed inside a function only.
  • Global scope: here variables are declared and accessed anywhere on script.

Example:

<?php

//global scope 

$x = 10; 

function var_scope() 

//local scope 

$y=20; 

echo "The value of x is :  $x "."
"; 

echo "The value of y is :  $y"."
"; 

var_scope(); 

echo "The value of x is :  $x"."
"; 

echo "The value of y is :  $y "; 

?>   

Output

The value of x is :

The value of y is : 20

The value of x is : 10

The value of y is :

There are two echo statements inside var_scope() function. It prints the value of $y as it is the locally declared and can not prints the value of $x since it is created outside the function.

 

The next statement of the script prints the value of $x since it is global variable i.e. not created inside any function.

 

The last echo statement can not prints the value of $y since it is local variable and it is created inside the function var_scope() function.

 

Global keyword

The global keyword is used to access a global variable within a function.

Example:

<?php

$x = 5;

$y = 10;

function myTest() {

    global $x, $y;

    $y = $x + $y;

}

myTest();

echo $y;

?>

Output: 15

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

Example:

<?php

$x = 5;

$y = 10;

function myTest() {

    $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];

}

myTest();

echo $y;

?>

Output: 15

 

Static Keyword

Generally, when a function terminates, all of its variable loose its values. If you want that variables hold these values for further use, you have to declare this variable as static.

“static” is predefine keyword used to making any variable static.

Example:

<?php

function myTest() {

    static $x = 0;

    echo $x;

    $x++;

}

 

myTest();

myTest();

myTest();

?>

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Output:

0

1

2

Next Topic