Variable scope

Each variable has an area in which it exists, known as its scope . It is technically possible for a PHP script to have several variables called $a in existence at one point in time, however there's only one active $a at any one time.

Any variables not set inside a function or an object are considered global - that is, they are accessible from anywhere else in the script that is not inside another function or an object. We'll be looking at function and object scope later on - for now, it is just necessary to understand that it is possible to have multiple variables of the same name.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Variable variables >>

Previous chapter: Non-decimal number systems

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.