Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> Introduction to Functions: Part 2 [Bookmark it]
Introduction to Functions: Part 2
PHP Foundations

Introduction to Functions: Part 2

08/09/2001

Today we'll revisit our discussion of functions and introduce several new topics, including scope, static variables, and return values.

The last time we spoke about functions, we took a fairly basic look at how they work and how they can be used in your scripts. This included declaring functions both with and without parameters, as well as how to declare default values for any parameters that the function may require. Today we'll extend on that discussion and explain many advanced topics dealing with functions. To begin, we'll start with the concept of function scope and how it can affect you when developing your own functions.

Variable Scope

For those of you with programming experience, the term "scope" may already be familiar to you. For those who are not familiar with the term "scope," it refers to the concept that a variable inside of a function cannot be inherently accessed by parts of the script which reside outside of that function. To better illustrate this, let's take a look at a bit of color-coded code and an associated diagram:

<?php

  function myfunction() {

   echo $foo;

  }



  $foo = "Hello";

  myfunction();

  echo $foo;

?>

When this code is executed, what will the output be? A reasonable answer to this question would be to say that we would echo the value of $foo twice, and hence the string "Hello" would be outputted twice to the browser -- but this is not the case. Although the variable $foo will be outputted twice to the browser, the result will only be a single "Hello." The reason this occurs is because the variable $foo inside of myfunction() is a completely different variable from the one outside of the function. That is to say, the "scope" of $foo within myfunction() is local to that function and separate from any other variables named $foo that reside outside of that function. This separation of same-name variables inside and outside a function is an important characteristic trait of PHP scripts. In order to access the variable $foo from within myfunction(), we'll need to introduce a new statement: global.

The Global Statement

The global statement's purpose is to allow a developer to access a variable outside the scope of a given function. The syntax for global is as follows:

global $var1, $var2, $var3, .... ;

Where $var1, $var2, etc. represent the different variables you which to access from within your function. To demonstrate how global works, we'll return to our example above and use the global statement to fix our bug and make the script output the contents of $foo twice:

<?php

  function myfunction() {

   global $foo;

   echo $foo;

  }



  $foo = "Hello";

  myfunction();

  echo $foo;

?>

Because of the use of the global statement in myfunction(), we have instructed PHP to treat all references to the variable $foo inside of the function the same as it would as if it was not a part of the function at all and within the same scope as the rest of the script. The result is the variable $foo will be outputted to the browser twice, once in the normal part of the script and then again in the function that the script calls.

[1] [2] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    Scaling Dynamic Websites w
    Whats New in ModSecurity
    A Day in the Life of #Apac
    Important Notice for Apach
    Custom-Compiling Apache an
    Apaches eXtended Server Si
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial