Global scope is a variable which declared outside a function and can only be accessed outside a function.
Example :
<?php
$x = 5; // global scope
function mindstick() {
echo "<p>Variable x inside function is: $x</p>";
}
mindstick();
echo "<p>Variable x outside function is: $x</p>";
?>
Local Scope:
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.
Example :
<?php
function mindstick() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
mindstick();
echo "<p>Variable x outside function is: $x</p>";
?>
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Global Scope:
Global scope is a variable which declared outside a function and can only be accessed outside a function.
Example :
Local Scope:
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.
Example :