Variables and Location, Location Location

While taking a review class on PHP, I heard the most ridiculous thing imaginable about variables… All the variables should be declared at the top of the first PHP tag or at the top of the page… Lets dig into this and see….

According to Code Complete: A Practical Handbook of Software Construction, Second Edition, you want to make the life of the variable as short as possible, (I mentioned this in my previous post Function Layouts, the best number of functions, and it still holds true today, but lets look at some of the Pro’s and Con’s.

So the concept is that if all the variables are defined at the top, that makes it very easy to locate them and change them if necessary. This seems fairly awesome, for small pages and infrastructures, however, the larger constructs will have some serious issues. Let’s assume that we have a page that is over 1,000 lines of code, all mixed with PHP and HTML, at the top is over 100 lines of code defining variables and values associated with those variables, and then the we decide to add a new variable that is used somewhere in the middle of the page. Making the change to it at the top makes crystal clear idea, going down to the place where we want to inject the code to read that variable is easy, but we then launch the page and we find out that the value that is presented to us is not the value we inserted into the code. The variable name we chose is already in use (if we bothered to read all 100 lines of code, we would have found it, perhaps a positive for keeping them all in the same place).

Take that same concept, and if we went down to the area that the variable was expected to be defined, we inject the variable, the output and usage within a short period of time, we risk overriding an already existing variable too, but we know the section of code we are working on is working fine. If the entire page also contains this limited scope and short time, the probability of the problems caused by our variable injection is lowered (not completely removed, but much lower).

With that same concept, if we have to change the value of that variable (if it already exists) we go searching for the surrounding text within the code and move upwards (hopefully very short distance) versus hunting through 100 lines of code that are mushed together for all variables.

Another positive take away of the Short Life concept, if we decide to later on refactor the section of code (say into an require_once event) having that variable close at hand will easily be found to be incorporated to the secondary file.

Until next time, Happy Coding and may you be blessed!


  1. I simply wanted to thank you very much once again. I am not sure what I would’ve worked on without the type of points contributed by you regarding this subject. It was before a troublesome dilemma in my opinion, however , viewing a skilled strategy you handled the issue forced me to weep with contentment. I’m thankful for this advice and expect you are aware of an amazing job you are getting into instructing most people all through a site. Most likely you have never encountered all of us.

  2. I happen to be writing to make you understand what a fine encounter our princess encountered going through your web page. She figured out some pieces, most notably how it is like to have an incredible teaching mood to let the others completely know some complex matters. You truly did more than her desires. Thanks for displaying the effective, healthy, educational and in addition easy tips on this topic to Lizeth.

  3. I simply wanted to write down a simple message to say thanks to you for the nice tips you are posting on this site. My long internet investigation has at the end of the day been honored with good facts and strategies to talk about with my relatives. I ‘d admit that we visitors actually are undeniably lucky to dwell in a perfect network with many special professionals with beneficial concepts. I feel rather grateful to have used the web page and look forward to so many more cool minutes reading here. Thank you once again for everything.

  4. I intended to send you this little observation to help thank you so much as before for your wonderful advice you have shown on this site. This has been really remarkably open-handed with you to provide without restraint precisely what many of us could possibly have made available for an ebook to make some money for their own end, chiefly since you could have tried it in the event you considered necessary. The tactics likewise served to become a good way to fully grasp that many people have a similar dreams like mine to figure out a great deal more on the topic of this problem. I am certain there are many more enjoyable moments up front for those who look into your website.

  5. Thank you a lot for giving everyone such a memorable opportunity to discover important secrets from this site. It’s always very pleasant plus full of a great time for me personally and my office acquaintances to search the blog at least thrice weekly to learn the newest things you have got. Of course, I am also certainly fulfilled with your extraordinary tricks you serve. Selected 4 ideas in this post are particularly the simplest I’ve ever had.

  6. I as well as my guys were actually checking out the great tips and hints found on your site and at once came up with a horrible feeling I had not thanked the web site owner for those techniques. These boys had been consequently happy to read them and have in effect unquestionably been loving these things. We appreciate you genuinely quite accommodating and also for using such outstanding resources most people are really desirous to be informed on. My very own sincere regret for not expressing appreciation to earlier.

  7. I enjoy you because of all of your hard work on this blog. My niece takes pleasure in making time for investigations and it is obvious why. Many of us learn all of the compelling method you make both interesting and useful guides by means of this blog and as well as attract response from other individuals on the article and our favorite princess is in fact studying a great deal. Enjoy the remaining portion of the year. You are performing a stunning job.

  8. I simply wanted to thank you so much once again. I do not know the things that I could possibly have handled in the absence of the actual recommendations shown by you relating to my question. It has been the frightening crisis for me personally, but seeing your professional fashion you solved it took me to cry over fulfillment. I’m just happier for this support as well as have high hopes you recognize what a powerful job you have been carrying out training many others by way of a web site. Probably you haven’t met any of us.

  9. I have to convey my affection for your kind-heartedness giving support to persons that really want assistance with this matter. Your real dedication to getting the message across has been amazingly powerful and has usually permitted most people much like me to achieve their objectives. Your personal warm and friendly suggestions denotes a lot to me and even more to my office colleagues. Warm regards; from each one of us.

  10. Thanks for your own hard work on this blog. Ellie delights in working on research and it is obvious why. Many of us learn all concerning the lively manner you give simple secrets via the website and therefore encourage contribution from others about this subject while my girl is now being taught a lot of things. Have fun with the remaining portion of the year. You’re the one carrying out a great job.

  11. Thanks a lot for giving everyone remarkably nice opportunity to check tips from this site. It is usually very lovely and also packed with fun for me personally and my office fellow workers to search your blog on the least three times weekly to read through the newest guidance you have. And of course, we’re actually fascinated considering the sensational concepts served by you. Certain 1 ideas in this article are completely the very best I have ever had.

  12. I must show thanks to you just for bailing me out of this particular crisis. After exploring throughout the world wide web and meeting principles that were not pleasant, I believed my life was gone. Being alive devoid of the answers to the difficulties you’ve solved through your short post is a serious case, as well as ones which could have in a wrong way damaged my career if I had not encountered your blog. That knowledge and kindness in playing with all things was important. I’m not sure what I would have done if I had not encountered such a point like this. I can at this time look forward to my future. Thank you very much for this high quality and effective guide. I won’t think twice to recommend the website to any individual who needs to have guidance about this situation.

  13. I am glad for writing to let you know what a magnificent encounter my friend’s princess undergone using your site. She even learned plenty of things, which included what it’s like to have an awesome coaching nature to let many people very easily know just exactly chosen extremely tough topics. You really surpassed readers’ desires. Thanks for supplying these invaluable, dependable, informative and in addition easy guidance on this topic to Tanya.

Leave a Reply

Your email address will not be published. Required fields are marked *