Difference between Local variable and Global variable In PHP

The scope of a variable is the part of the script where the variable can be referenced/used. The scope of a variable is the portion of the program within which it is defined and can be Difference between Local variable and Global variable In PHP accessed.

1. Local variable:

A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function.

Example:

<?php

     function Local() { 
        $n = 90; // local scope
      echo "Local function is  $n. <br />";
   }
   
 echo local();

?> 

2. Global variable

The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. These variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.

Example:

<?php
$a = 4;
$b = 14;

function testGlobal() {
  global $a, $b;
  $b = $a + $b;
} 

testGlobal();  // run function
echo $b; // output the new value for variable $c
?>

How to get product salable quantity by object manager in Magento 2expoundsofttechsolution

Difference between Local variable and Global variable In PHP