

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Page 1 function greet () { $str = 'Hello from Greet'; echo $str; }
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.
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.
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.
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.
$str = 'Hello from PHP'; function greet () { echo $str; // This will not work because $str is not in scope }