PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


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.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: