phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP Basic: Variable Scope

Tag: basic, variable, variable scope   Category: PHP Basic
post: 01 Mar 2008 read: 211


PHP Basic Data Type Step By Step Tutorial - Part 12: Variable scope is a context where the variable defined. In principle, variable in PHP have single scope. Variable scope is differentiated become three type, there are:
- global variable
- local variable
- variable of static

Global variable

Global variable is a variable that is having a global character and it is able to be recognized and used by the entire part of script. In PHP, global variable which will be used in the function must have to declare first in global in its function. Pay attention to the example of using variable global in script as following:
File name: scopevariable.php


<html>
<head>
<title>Variable Scope</title>
</head>

<body>
	<h1>Variable Scope</h1>
<?	
	$x = 25; //global variable
	$y = 30; //global variable
	$z = 0;
	function Sum()
	{
		global $x, $y, $z;
		
		$z = $x + $y;
	}
	Sum();
	echo "\$x = $x"."<br>";
	echo "\$y = $y"."<br>";
	echo "\$x + \$y = $z";
?>
</body>
</html>
php basic variable scope

To access the global variable, you can also use $GLOBALS array variable. $GLOBALS array is good for noting all global variable in script.
Example:
File name: scopevariable1.php


<html>
<head>
<title>Variable Scope</title>
</head>

<body>
	<h1>Variable Scope</h1>
<?	
	$x = 25; 
	$y = 30; 
	$z = 0;
	function Sum()
	{
		$GLOBALS ['z'] = $GLOBALS ['x'] + $GLOBALS ['y'];
	}
	Sum();
	echo "\$x = $x"."<br>";
	echo "\$y = $y"."<br>";
	echo "\$x + \$y = $z";
?>
</body>
</html>
php basic variable scope

Local variable

Local variable is variable which defined in a function so that the variable have the character of can only recognize and used in function which variable declaration. Local variable can have the name of which equal to the global variable.
File name: scopevariable2.php


<html>
<head>
<title>Variable Scope</title>
</head>

<body>
	<h1>Local variable</h1>
<?	
	$day = "Sunday"; // global variable
	function test()
	{
		$dy ="Monday"; // local variable
		echo $day; // local variable
		echo $dy;
	}
	test();
?>
</body>
</html>
php basic variable scope

Static Variable

Static variable is variable which there's only in local scope of a function. Variable do not eliminate the last value after finish to be executed and leave the function. It means that the last value after the executing which saves in the variable is not change until it will be called again.

Example of using variable without static at script as following:
File name : scopevariable3.php


<html>
<head>
<title>Variable Scope</title>
</head>

<body>
	<h1>Static variable</h1>
<?	
	function test()
	{
		$x = 0;
		echo " \$x = $x"."<br>";
		$x++;
	}
	test();
	test();
	test();
	test();
?>
</body>
</html>
php basic variable scopt

In the script above, it indicates that the last value of $a variable do not be defended, and it back sets into 0. You can see the difference of it by seeing the next script which use static. Variable of static is usually used as counter. For example, in noting the amount of visitor at one particular web situs.

The example of using variable with static at script as following:
File name: scopevariable4.php


<html>
<head>
<title>Variable Scope</title>
</head>

<body>
	<h1>Static variable</h1>
<?	
	function test()
	{
		static $x = 0; // with satatic
		echo "\$x = $x"."<br>";
		$x++;
	}
	test();
	test();
	test();
	test();
?>
</body>
</html>
php basic variable scope



| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting