PHP Variable Scope Local, Global and Static in Function Example Code

PHP Variable Scope

The scope is a boundary or limit where you can access the variable and it depends on the type of the variable. PHP supports local, global and static variable types.

Local Variable Scope

Local Scope means the variable is accessible within the same function where it is created.

Example 1 – Local Variable Scope Example in function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$x = 10;
		function test()
		{
			$x = 4;
			echo "The value of inside x is " . $x . "<br />";
		}
		test();
		echo "The value of outside x is " . $x;
		
	?>
  </body>
</html>

In the above example, we declare $x variable but the value this $x is different The below screenshot shows the result of the above code.

PHP Local Variable Scope
Fig.1 – PHP Local Variable Scope.

PHP Global Variable Scope in Function

Global Scope means the variable is accessible within the same page where it is created. When you used the global keyword with the variable then that variable will be accessed within that scope.

Example 2 – Global Variable Scope Example in function called test

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$x = 5;
		$y = 6;
		
		function test()
		{
			global $x,$y;
			$y = $x * $y;
			echo "The value of y is " . $y . "<br />";
		}
		test();
		echo "Now the outside value of y is " . $y;
	?>
  </body>
</html>

In the above example, we declare two variables $x & $y and also create a function. And we print the value of $y after calculating the function. The below screenshot is the result of the above code.

PHP Global Variable Scope Example in function
Fig.2 – PHP Global Variable Scope.

PHP Static Variable Scope in Function

A static variable keeps the value within the function during the last execution of the function call. For example, if suppose we have a static variable within the function and the value of the variable is 10 when the function is terminated than in the next function call the variable value will be 10.

Example 3 – How to use Static Variable in function

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		function test()
		{
			static $x = 5;
			$y = 10;
			$x = $x + $y;
			echo "The value of x is " . $x . "<br />";
		}
		test();
		test();
		test();
	?>
  </body>
</html>

In the above example, we declare two variables $x & $y and also create a function text. And we print the value of $x after calculating the calculation of the function text. And we call this function for 3 times and we get the result with is shown in the below screenshot.

PHP Static Variable Scope in Function
Fig.3 – PHP Static Variable Scope.
You can leave a response, or trackback from your own site.

One Response to “PHP Variable Scope Local, Global and Static in Function Example Code”

  1. I think the admin of this website is genuinely working hard in support of his site, since here every material is quality based data.|

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!