PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


PHP Basic: Variable Scope


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




Tag: basic, variable, variable scope Category: PHP Basic Post : March 01st 2008 Read: 888 Bookmark and Share

blog comments powered by Disqus

Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration