PHP variable scope and differ it from Javascript variable scope
In PHP and Javascript Variables have a limited "scope" which means that variable has a limited place where they are accessible. for example, if we defined $x=5; once somewhere in our application, it doesn't mean we can refer to $x from everywhere inside the application. $x has a limited scope within which it is valid and only code in the same scope has access to the variable.
The main difference b/w Javascript and PHP variable scope is observing their accessibility inside and outside of functions:
- Javascript: Global variables are accessible inside the functions if defined outside of a function. There is an outer reference feature built into the engine that allows for variables in the outer environment to be visible and available in the execution context of a function.
- To know more about Javascript variable scope and the differences between them click here
//Global scope variable
var name = "Abdul";
function returnName(){
//use variable inside of a function because name var is global scope
console.log(name);
//it returns Abdul
}
//use variable outside of a function because name var is global scope
console.log(name);
//it also return Abdul
// call that function
returnName();
<?php
$name = 'Adbul'; // global scope
function myTest() {
// using $name inside this function will generate an error
echo "<p>Variable x inside function is: $name</p>";
}
myTest();
echo "<p>Variable x outside function is: <b> $name </b></p>";
?>
PHP Global variable with global keyword:
PHP has a global keyword to access the global scope variable inside the function
<?php
$name = 'Adbul'; // global scope
function testFn() {
global $name; // now $name can be access here
echo "<p>Variable x inside function is:<b> $name </b> </p>";
}
testFn();
echo "<p>Variable x outside function is: <b> $name </b></p>";
?>
Now we will discuss PHP variable scope:
The scope of a variable in the programming language is the part of the script in which place the variable can be referenced or used.
In PHP there are three types of variable scope:
- global scope
- local scope
- static scope
Global scope
A variable declared outside of a function has a GLOBAL SCOPE and can only be accessed outside a function and we can also assess it inside the function using the global keyword as discussed above
<?php
$country = "india"; // global scope
function testFn() {
// using country inside this function will generate an error
echo "<p>Variable x inside function is: $country</p>";
}
testFn();
echo "<p>Variable x outside function is: $country</p>";
?>
Local scope
A variable declared within of a function has a LOCAL SCOPE and can only be accessed within that function:
<?php
function testFn() {
$country = "india"; // local scope
echo "<p>Variable x inside function is: $country</p>";
}
testFn();
echo "<p>Variable x outside function is: $country</p>";
?>
Static scope
Generally, when a function is completed or executed, all of its variables are deleted. But, sometimes we would like a local variable NOT to be deleted. We need it for further operations.
for doing this, we use the static keyword when we first time declares the variable:
<?php
function testFn() {
static $num = 10;
echo $num;
$num++;
}
testFn();
testFn();
testFn();
?>