In this lesson our instructor talks about variable scope. First, he discusses variable scope, global scope, and local function scope. Then he talks about global keyword, superglobals, and pitfalls of global variables. He also lectures on when to define variables and function scope. He ends the lesson with a helpful homework challenge.
Variable scope refers to the part of a script where a variable can be
Variables defined in a script outside of a function have global scope . A
variable with global scope can be used anywhere within the script it is defined or
the files included in the script it is defined (*except within functions).
Variables defined in functions, including function parameters, have what is known
as local function scope . Variables with local function scope can only be
accessed from within the function.
The global keyword allows variables with global scope to be used within
functions and variables with local function scope to be used in the global
Superglobal variables are pre-defined associative array variables that are
always available in both the global scope and local function scope.
The $GLOBALS superglobal contains all of the variables currently in the
global scope of a script. It can be used to access global variables from within local
All constants are accessible in both the global and local function scope.
Functions have global scope, meaning as long as a function is defined within a
script or one of its include files, it can be called anywhere within that script or
its include files.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.