Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 7. Functions > Understanding Variable Scope in Functions

Understanding Variable Scope in Functions

PHP functions work differently than many other programming languages because they are globally defined. Likewise, the rules of scope may appear different than the norm, but they are not.

Functions can access only global variables and variables defined in them, which are known as local variables. You define global variables with the define() function that was discussed in Chapter 4. Global variables can be defined inside functions, which make them available only after calling the containing function. This behavior has benefits and problems. The benefit is that you can define conditional global variables at run time. The problem is that you can have more than one mechanism that returns values from a function, which is a potential control problem.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial