PHP Variable Scope Global Static & Local

Today, We want to share with you PHP Variable Scope Global Static & Local.
In this post we will show you PHP Variable Scope Example, hear for PHP Global Static & Local Variables we will give you demo and example for implement.
In this post, we will learn about scope – PHP access global variable in function with an example.

PHP Variable Scope Global Static & Local

There are the Following The simple About PHP Variable Scope Global Static & Local Full Information With Example and source code.

In PHP source code, variables can be initlized or declared anywhere any function in the script.PHP has main three types of the different variable scopes like

  • global
  • static
  • local

outside variable declared -> GLOBAL SCOPE

<?php
$x = 15; // global scope

function liveScopeVarible() {
    // using x inside this function will generate an error
    echo "<p>PHP Variable Scope x inside function is: $x</p>";
} 
liveScopeVarible();

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

within PHP Variable Scope declared -> LOCAL SCOPE
<?php
function liveScopeVarible() {
    $x = 15; // local scope
    echo "<p>PHP Variable Scope x inside function is: $x</p>";
} 
liveScopeVarible();

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

1. Local Scope Variables


<?php
function calculate_flags() {
$flag = 15;
//will print 15; the value of local PHP Variable Scope
echo $flag++; 
}
?>

2. Global Scope Variables

PHP main global custom PHP variables may be defined by simple keyword using global keyword

<?php
$flag = 0;
function calculate_flags() {
	global $flag;
	// will print 0 and increment $flag global variable
	echo $flag++ . "<br/>"; 
}
calculate_flags();
echo $flag;
?>

<?php
$flag = 0;
function calculate_flags() {
// will echo 0 and simple increment flag global PHP variable initlized outside this function
echo $GLOBALS["flag"]++ . "<br/>"; 
}
calculate_flags();
echo $flag;
?>

3. Static Variables (local scope)

A simple PHP static variable is newar by again a php variable with own local scope

Read Also:  Laravel 6 Get Last Inserted ID using Eloquent

A variable may be about init by using the keyword of the ‘static’ keyword a PHP function.

<?php
function counter()
{
    static $flag = 0;
    echo $flag;
    $flag++;
}
?>

4. Function Parameters (Local Scope)

Scope and File Includes

The Last of the files inserted does not any data demarcate the PHP scope of variables

Function Inside Function or Class

Remember that the root main scope in server side PHP scripts is governed by a simple PHP function block

Read Also:  Laravel Search Multiple Columns

<?php
function foo() {
    $student = 'jaydeep';

    $bar = function () {
        // $student cannot be accessed inside here
        $teacher = 'Ankit Sir';
    };

    // $teacher cannot be accessed outside here
}
?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Global Static & Local Variables.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.