Docsity

Understanding Variable Scope in PHP Functions, Lecture notes of Computer science

Toggle sidebar
bg1
Page 1
function greet() {
$str = 'Hello from Greet';
echo $str;
}
D
Functions
and
Variable
Scope
What are Local Variables?
A local variable is a variable that is defined within a function and is only accessible
within that function.
In the context of functions, local variables are also known as local scope variables. These
variables are only known by the function in which they are defined and are not accessible
outside of that function.
Example: Local Variables in a Function
Consider the following example:
In this example, the variable $str is a local variable because it is defined within the greet
function. This variable is only accessible within the greet function and is not accessible outside
of it.
What are Global Variables?
A global variable is a variable that is defined outside of a function and is accessible
from anywhere in the program.
In contrast to local variables, global variables are defined outside of a function and are
accessible from anywhere in the program. However, global variables are not accessible within a
function unless they are explicitly passed as arguments to the function.
Example: Global Variables and Functions
Consider the following example:
pf2

This document is temporarily unavailable for download

Partial preview of the text

Download Understanding Variable Scope in PHP Functions and more Lecture notes Computer science in PDF only on Docsity!

Page 1 function greet () { $str = 'Hello from Greet'; echo $str; }

D Functions and Variable Scope

What are Local Variables?

A local variable is a variable that is defined within a function and is only accessible within that function. In the context of functions, local variables are also known as local scope variables. These variables are only known by the function in which they are defined and are not accessible outside of that function.

Example: Local Variables in a Function

Consider the following example: In this example, the variable $str is a local variable because it is defined within the greet function. This variable is only accessible within the greet function and is not accessible outside of it.

What are Global Variables?

A global variable is a variable that is defined outside of a function and is accessible from anywhere in the program. In contrast to local variables, global variables are defined outside of a function and are accessible from anywhere in the program. However, global variables are not accessible within a function unless they are explicitly passed as arguments to the function.

Example: Global Variables and Functions

Consider the following example:

Page 2 Variable Type Accessibility Global Variable Anywhere in the program, but not within a function unless explicitly passed as an argument Outside a function Only within the function Within a function Local Variable Scope In this example, the variable $str is a global variable because it is defined outside of the greet function. However, the greet function does not have access to this variable because it is not in scope.

Variable Scope Table

Key Points to Remember

Local variables are defined within a function and are only accessible within that

function.

Global variables are defined outside of a function and are accessible from

anywhere in the program, but not within a function unless explicitly passed as an

argument.

Functions create their own scope, and variables defined within a function are not

accessible outside of that function.

$str = 'Hello from PHP'; function greet () { echo $str; // This will not work because $str is not in scope }