Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.6     Variable scope

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 3.5 Non-decimal number systems   3.7 Variable variables >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
mystic@de4th.com , again - 29 Aug 2008

Sorry..

In my opinion, the first is more legible and the usage of multiple variables

should be

In my opinion, the first is more legible and the usage of multiple variables *of the same name*

mystic@de4th.com - 29 Aug 2008

I wouldn't consider it so much a bad programming habit when used in the context of functions. For example, I find it handy to use multiple $a's, $b's, etc within my functions because it's an easier and faster method of computing. Like the following:

function myFun($a,$b,$c) //higher num, lower num, exponent
{
$result = round(pow(($a / $b),$c));
return $result;
}

compared to:

function myFun($high,$low,$expo)
{
$result = round(pow(($high / $low),$expo));
return $result;
}

In my opinion, the first is more legible and the usage of multiple variables can be justified when comments are supplied to explain what said variables are. This allows multiple functions to use the same variables and improve readability, thanks to their small scope. It would be a pain naming every dynamic variable something different.

A PHP User - 29 Aug 2008

Perhaps this is true but why would you want to encourage bad programming habits?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow