in ,

PHP Variables Scope

PHP Variables Scope
PHP Variables Scope

In this tutorial, you will learn about PHP Variables Scope step by step. So without much to do, let’s get started.

In this tutorial, you will learn-

In this article, you will learn-

PHP Variables Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable is the a part of the script in which the variable can be referenced/used.

PHP has three distinctive variable scopes:

• local
• global
• static


Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE and can simplest be accessed outside a function:

Example
Variable with global scope:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 5; // global scope
 
function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>

A variable declared inside a characteristic has a LOCAL SCOPE and can be accessed inside that function:

Example
Variable with local scope:

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
  $x = 5; // local scope
  echo "<p>Variable x inside function is: $x</p>";
} 
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

</body>
</html>

You can have local variables with the same name in different functions, because local variables are most effective recognized by means of the function wherein they are declared.

PHP The global Keyword

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

To do this, use the global keyword before the variables (inside the function):

Example

<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
  global $x, $y;
  $y = $x + $y;
} 

myTest();  // run function
echo $y; // output the new value for variable $y
?>

</body>
</html>

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

The instance above may be rewritten like this:

Example

<!DOCTYPE html>
<html>
<body>

<?php
$x = 5;
$y = 10;

function myTest() {
  $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
} 

myTest();
echo $y;
?>

</body>
</html>

PHP The static Keyword

Normally, whilst a function is finished/executed, all of its variables are deleted. However, now and again we want a local variable NOT to be deleted. We need it for a further activity.

To do that, use the static keyword when you first declare the variable:

Example

<!DOCTYPE html>
<html>
<body>

<?php
function myTest() {
  static $x = 0;
  echo $x;
  $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?> 

</body>
</html>

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

Note: The variable remains local to the function.


READ NEXT

PHP Introduction

PHP Installation

PHP Syntax

PHP Comments

PHP Variables


This is about PHP Variables Scope, and we hope you have learned something from this tutorial and share your opinion about this tutorial. What do you think about it, and if you think this tutorial will help some of your friends, do share it with them.


salman khan

Written by worldofitech

Leave a Reply

PHP Variables

PHP Variables

PHP echo and print Statements

PHP echo and print Statements