PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
There are three types of variable scopes in PHP:
Let's understand each one with an example.
1. Local Scope
If you declare a variable inside a function, it is a local variable and can only be used inside that function.
Example:
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>";
In the above example, the echo statement outside the function will result in an error because $x is a local variable to the myTest()
function and it's not accessible outside of it.
2. Global Scope
A variable declared outside a function has a global scope and can only be accessed outside a function.
Example:
$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>";
In the above example, the echo statement inside the function will result in an error because $x is a global variable and is not accessible within the myTest()
function.
To access a global variable inside a function, you need to use the global
keyword:
$x = 5; // global scope $y = 10; // global scope function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // outputs 15
In the above example, $x and $y are global variables. This is indicated in the myTest
function with the global
keyword before the variables. Therefore, the function is able to access the global variables and perform the addition operation.
3. Static Scope
If you declare a variable as static inside a function, the variable will remember its value each time the function is called. This is unlike a typical local variable which loses its value when the function's execution is completed.
Example:
function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest();
In the above example, the variable $x is declared as static. This means that it retains its value between function calls. So, the first time myTest()
is called, it outputs '0'. The second time, it outputs '1', and the third time, it outputs '2'.
That is a brief overview of variable scope in PHP. Understanding variable scope is very important for writing effective PHP code.
Local and global variables in PHP:
<?php $globalVar = "I'm global"; // Global variable function myFunction() { $localVar = "I'm local"; // Local variable echo $localVar; } myFunction(); echo $globalVar;
Static variables and scope in PHP:
<?php function counter() { static $count = 0; $count++; echo $count; } counter(); // Outputs: 1 counter(); // Outputs: 2
PHP variable scope within functions:
<?php function myFunction() { $functionVar = "Inside function"; echo $functionVar; } myFunction();
PHP variable scope in classes:
<?php class MyClass { public $classVar = "I'm a class variable"; public function printVar() { echo $this->classVar; } } $obj = new MyClass(); $obj->printVar();
Scope resolution operator in PHP:
<?php $globalVar = "I'm global"; // Global variable function myFunction() { global $globalVar; echo $globalVar; } myFunction();
Dynamic variable scope in PHP:
<?php $scope = "global"; function myFunction() { global $scope; echo $scope; } myFunction();
PHP variable scope and include/require statements:
<!-- File: config.php --> <?php $configVar = "Configuration variable";
<!-- File: index.php --> <?php include "config.php"; echo $configVar;
PHP variable scope in loops:
<?php for ($i = 0; $i < 5; $i++) { $loopVar = "Iteration $i"; echo $loopVar . "<br>"; }
Changing variable scope in PHP:
<?php function changeScope() { $variable = "Local variable"; return $variable; } $globalVariable = changeScope(); echo $globalVariable;
Nested functions and variable scope in PHP:
<?php function outerFunction() { $outerVar = "I'm outer"; function innerFunction() { global $outerVar; echo $outerVar; } innerFunction(); } outerFunction();
PHP variable scope and closures:
<?php $outerVar = "I'm outer"; $closure = function() use ($outerVar) { echo $outerVar; }; $closure();