Broad Network


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

Introduction

This is part 1 of my series, PHP Variable Scope. In this part of the series, I talk about PHP function scope.

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 forchatrans@yahoo.com .

Pre-Knowledge
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:

<?php

    function fn()
        {
            $b = 2;
            echo $b;
        }

    fn();

?>

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:

<?php

    $b = 2;

    function fn()
        {
            //some statements . . .
        }

    echo $b;

?>

The variable, $b is declared and used outside the function. This variable cannot be used (seen) inside the function. It is said to have Global Scope.

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:

<?php

    function fn()
        {
            $b = 2;
        }

    echo $b;

?>

What about a variable as a function parameter? Consider the following program:

<?php

    function fn($b)
        {
            echo $b;
        }

    fn(5);

?>

$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.

<?php

    function fn($b=3)
        {
            //some statements
        }

    echo $b;

?>

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.

Chrys

Related Links

Basics 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
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
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 Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message