PHP Function Scope
PHP Variable Scope – Part 1
Foreword: In this part of the series, I talk about PHP function scope.
By: Chrysanthus Date Published: 25 Jan 2014
Note: the output of all the code samples in this series are to the browser.
Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, or just want to comment, contact me at email@example.com .
There are certain topics you need to have covered before you can understand this series. Click the link titled, “PHP Course” below to know what you should have studied before reaching here (PHP Variable Scope). This series is part of my volume, PHP Course.
My PHP course is divided into two sub-courses: the professional course and the advanced course. By the time you complete the advanced course, you will be able to read the PHP manual (specification) and understand and you will be a master in PHP. That is on condition that you complete the whole course and you understand most of the topics in the course.
What is a Variable Scope
The PHP specification states: “The scope of a variable is the context within which it is defined.” You may not understand what that means if you are new to programming. So I will do some illustration, as I explain function scope. Consider the following program:
$b = 2;
The variable, $b is declared and used only in the function block. This variable cannot be seen (used) outside the function block. This variable is said to have the Local Function Scope or just, function scope. Consider now the following program:
$b = 2;
//some statements . . .
The following statement will not output any value for $b because $b has been declared inside the function block and not in the global scope:
$b = 2;
What about a variable as a function parameter? Consider the following program:
$b is a parameter to the function. Any variable in the parameter list can be seen (used) inside the function block. The echo statement inside the function block has used the parameter, $b.
Can a function parameter be seen in the global scope? In the following program, nothing is outputted.
This is because a variable in the parameter list can be seen in the function block and NOT outside the function.
That is it for this part of the series. We stop here and continue in the next part.
Related LinksBasics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
Date and Time in PHP
PHP String Functions for Website Design
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Web Application
More Related LinksMajor in Website Design
Web Development Course