Before leaving the topic of PHP variables, it’s worth taking a look at how to name variables. All PHP variable names have to follow these rules:
  • They start with a $ (dollar) symbol.
  • The first character after the dollar must be either a letter or a _ (underscore) symbol.
  • The other characters may be letters, numbers, and/or underscores.
Variable names can be as long as you like, but it’s a good idea to keep them relatively short (otherwise they can get quite unwieldy). It’s also good to use meaningful variable names — for example, $numWidgets is much more useful than $nw.
PHP variable names are case-sensitive, which means that $myVariable and $MyVariable refer to different variables.
Which of the following are valid PHP variables?
@$foo
&$variable
${0}
${1}
${0×0}
${0y0}
${030}
$variable
$0×0
${0*0}
${0+0}
${0-0}
${0/0}
${0%0}

Variable scope

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:
b.inc file
<?php
$a=2;
?>
<?php
$a = 1;
include 'b.inc';
echo $a;
?>

Here the $a variable will be available within the included b.inc script
Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:
<?php
$a = 1; /* global scope */

function test()
{
echo $a; /* reference to local scope variable */
}

test();
?>

This script will not produce any output because the echo statement refers to a local version of the $avariable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.

THE GLOBAL KEYWORD

First, an example use of global:
Example #1 Using global
<?php
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
?>

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as:

Example #2 Using $GLOBALS instead of global
<?php
$a = 1;
$b = 2;

function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;
?>

USING STATIC VARIABLES

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example:
<?php
function test()
{
$a = 0;
echo $a;
$a++;
}
test(); // Output will be 0
test();// Output will be 0
test();// Output will be 0
?>

This function is quite useless since every time it is called it sets $a to 0 and prints 0. The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static:

<?php
function test()
{
    static $a = 0;
    echo $a;
    $a++;
}
test(); // output should 0
test(); // output should 1
test(); //output should 2
?>

Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

0 comments:

Post a Comment

 
Top
Blogger Template