PHP Tutorial
What is PHP Environment Setup of PHP PHP Syntax PHP Comments PHP Echo & print PHP Variables PHP Scope Variables PHP $ and $$ Variable PHP Constants PHP Operators PHP Data TypesPHP Conditional Statements
PHP Loop Type
PHP Arrays
PHP String PHP FunctionsPHP Form Examples
Form Intro Get & Post Form Validation / Required PHP Complete FormPHP Advanced
PHP Include / Require PHP Date & Time PHP Session PHP Cookies PHP File Handling PHP Open File PHP Read File PHP Write File PHP Append File PHP Delete File PHP File Upload PHP Sending Mail PHP Error HandlingPHP Scope Variables
The scope of a variable determines which part of the code can access it. The locations where the variable can be accessible determine the scope of the variable.
In PHP, variables have four types of scopes:
- Local
- Global
- Static
- Function parameters
Local variables
When you define a variable inside a function, you can only access that variable within the function. And it’s said that the variable is local to the function.
function welcome()
{
$message = 'Hi';
echo $message;
}
Inside the welcome()
function, we define the $message
variable. The $message
variable is a local variable. And you cannot access it from the outside of the welcome()
function.
Also, the $message
variable only exists during the execution of the welcome()
function. Once the welcome()
function ends, the $mesage
variable won’t exist anymore.
Global variables
When you declare a variable outside of a function, the variable is global. It means that you can access the variable anywhere within the script except inside a function.
For example:
$message = 'Welcome';
function welcome()
{
$message = 'Hi';
echo $message;
}
echo $message; // Welcome
In this script, we have two variables with the same name $message.
The first variable is the global variable because we define it outside of a function. The $message variable that we define inside the function is the local variable. Even though these variables have the same name, they’re two different variables.
Using $GLOBALS instead of global
Another way to use the global variable inside the function is predefined $GLOBALS array.
<?php
$num1 = 7; //global variable
$num2 = 13; //global variable
function global_variable()
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " .$sum;
}
global_variable();
?>
Output: Sum of global variables is: 20
Static variables
A static variable retains its value between function calls. Also, a static variable is only accessible inside the function. To define a static variable, you use the static keyword.
<?php
function get_count() {
static $count = 1;
return $count++;
}
echo get_count() . '<br>'; // 1
echo get_count() . '<br>'; // 2
echo get_count() . '<br>'; // 3
?>
Output:
1
2
3
How it works
First, define the get_count() function with a static variable named $count.
Second, call the get_count() function three times. As you notice that the value of the $count variable is increased by one after each function call.
Function parameters
Function parameters are local to the function. Therefore, function parameters can only be accessible inside the function.
For example:
<?php
function final($numbers) {
$total = 0;
foreach($numbers as $number) {
$total += $number;
}
return $total;
}
echo final([5,10,15]); // $items cannot be accessible here
?>
In this example, the $numbers is the parameter of the final() function. It can only be accessible within the final() function.
Elevenstech Web Tutorials
Elevenstech Web Tutorials helps you learn coding skills and enhance your skills you want.
As part of Elevenstech's Blog, Elevenstech Web Tutorials contributes to our mission of “helping people learn coding online”.
Read More
Newsletter
Subscribe to get the latest updates from Elevenstech Web Tutorials and stay up to date