Variable scope global vs. local variables php

Variable scope global vs. local variables php

In PHP program development, user-declared function variable scope refers to the control and access of variables used within a program, where programmers define the default access and processing of existing variables within the program. Generally, function variable scopes are of two types: local variable scope and global variable scope. PHP programmers can declare and use function local variable scope as needed for better modular programming coding.

Variable scope global vs. local variables php

Local Variables in PHP.

Always remember that in a PHP function program, a local variable is declared within a function, and can only be accessed and controlled from within that function. Function local variables are not accessible from outside the function where the local variable is defined.

Example of a local variable in PHP.

<?php

function disp_info() {

$info = “welcome to vcanhelpsu programming”; // declare as a local variable function

echo $info;

echo “\n”;

}

disp_info(); // Result – welcome to vcanhelpsu programming

echo $info; // Error – HP Notice: Undefined variable $info

?>

Local Variables in the PHP example.

  • In the local variable program example, $info is a locally declared function variable inside the disp_info() function.
  • Since local variables have local scope, they can only be accessed and controlled within the disp_info() function. An error is displayed if a local variable is accessed from outside the function.

Global Variables in PHP.

A global variable in a PHP function program is declared externally within a function, defined at the top of the PHP program script, and can be accessed and controlled anywhere within the program script. Programmers can reference or define global function variables within the function in the proper order.

Example of a global variable in a PHP function.

<?php

$company = “Vcanhelpsu”; // Here we declare a global variable $company

function disp_info() {

global $company; // Here we can access the global variable inside the declared function

echo “welcome to $company”;

}

disp_info(); // Result – welcome to Vcanhelpsu

?>

In the Global Variables example.

  • Here in the global function program, $company is a globally declared variable of type Nature.
  • Here, inside the disp_info function, we used the global keyword to access the global variable $company.

Accessing Global Variables Inside a PHP Function.

In PHP program development, if programmers want to access and control a global variable inside a function, they can either use the global keyword in the function program, or define or reference the function variable as $GLOBALS[‘variable_name’].

Use of the global keyword in a PHP function.

<?php

$course = “php programming”; // here declare $curse as a global variable

function disp_course() {

global $course; // here we use the global keyword to access the global variable element

echo “you select the $course”;

}

disp_course(); // Result – you select the PHP programming language

?>

Use of the $GLOBALS array in a PHP function.

In PHP function program development, there is a special associative array data type called $GLOBALS, which provides programmers with access and control over all global variables within a function, even those declared within the function.

<?php

$select = “hp laptop”; // here declare $select as the global variable

function select_model() {

echo “you select the ” . $GLOBALS[‘select’]; // here we access the global variable via the $GLOBALS associative array

}

select_model(); // Result – you select the hp laptop

?>

Superglobals Array in PHP.

In PHP function program development, programmers have access to several built-in global array features, known as superglobals function arrays. The declared functions are always available in the script, regardless of the scope of these functions.

Common superglobal features include.

  • $_GET – This superglobal function contains data sent to the script via the GET method (URL parameter).
  • $_POST – This superglobal function contains data sent via the POST method, typically a webpage or website form submission.
  • $_SESSION – Contains the session variables of the current function.
  • $_COOKIE – Contains the cookie data information of the current session (webpage or website).
  • $_SERVER – Contains the current server and server execution environment information.
  • $_FILES – Contains file upload information or data to the current system.
  • $_REQUEST – Contains the data information of the current webpage or website session ($_GET, $_POST, and $_COOKIE).

Example of using $_GET in PHP.

<?php

// here URL: script.php?company=vanhelpsu

echo “welcome to, ” . $_GET[‘company’];  // Result – welcome to vcanhelpsu

?>

Static variables in PHP.

In PHP function program development, a static variable maintains its value constant throughout multiple function calls. The declared static variable has a local scope. Programmers can declare it using the static keyword.

Example of a static variable in PHP.

<?php

function tester() {

static $increaser = 7; // here declare $increaser as a static variable

$increaser++;

echo $increaser;

}

tester(); // Result – 8 every time it increases tester’s value without 1

tester(); // Result – 9

tester(); // Result – 10

?>

Static variables in program state.

  • In this program, $increaser is a static local variable, so it maintains its static value throughout function program calls.
  • While a static variable is the opposite of a regular local variable, which is automatically reset every time a function is called, a static variable (like $increaser) retains its value from the previous function call.

A summary of variable scope in PHP functions.

  • Local variables – Local variables are defined within a function in PHP development and can only be accessed and controlled from within that function.
  • Global variables – Global variables declared externally within any function in PHP development are available throughout the program script, but access within the current function requires the global keyword or the $GLOBALS array.
  • Superglobals – These are predefined global arrays in PHP development, such as $_GET, $_POST, $_SESSION, etc., which are always available for user access.
  • Static variables – In PHP program development, static variables are of a local variable nature, which maintain their value static between function calls by using the static keyword.

Leave a Reply