• Home

CodingExperiments.com

$ sudo make money

Search

Category:

  • Apple Inc.
  • Facts
  • Fun
  • Google
  • Google Android
  • Ideas
  • Internet
  • Linux
  • Microsoft
  • Programming
  • Rants
  • Security
  • Uncategorized
  • web 2.0

Archives:

  • April 2010
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007

Pages

  • About
  • About
    • The Authors
  • Commenting your code
  • How to Write Papers with Groff
  • ModCMS Anti-Spam Component Set
  • ModCMS Technical Specifications
  • Regular Expressions Guessing Game
  • Saving code directly to a web server
  • The (Almost) Perfect PHP 404 Page

Meta:

  • RSS
  • Comments RSS

Awesomeness tracker

CodingExperiments at Blogged View blog authority
Free Page Rank Tool

Beginner PHP Programmer Mistakes Part One: Using Global Variables

June 22nd, 2008 by Rishabh Mishra

I’ve talked about PHP before. One of the major problems is all the beginners writing code and posting examples that other beginning programmers use. But complaining on and on about beginner code isn’t actually very useful. What if I actually explained how to write good code in PHP and what sort of code that a PHP programmer should avoid? Well, that would actually be useful. That would accomplish something. That would be another series of posts at CodingExperiments.

So, here goes. This post in the series will cover using global variables in PHP, or rather, why you shouldn’t use global variables in PHP. First, let’s see what a global variable is, and what it does.

<?php
$random_variable = 'foo';
$awesome_variable = 'http://codingexperiments.com';
// This works
function bar ()
{
// Making it global makes it work
global $random_variable;
echo $random_variable;
}
bar ();
//
// This doesn't
function website_promotion ()
{
// $awesome_variable is not being made global here
echo '
' . $awesome_variable;
}
website_promotion ();
?>

If you were to run that script, you would get “foo”, and only “foo”. Don’t believe me? Try it out here. Any good programmer will tell you that the URL of this awesome website isn’t being displayed because of scope. Read more about PHP variable scope here.

So, why are global variables bad? Because they can be changed from anywhere. Global variables are often used for configuration settings, as they will be needed in many places in the program. Configuration settings typically should never change. That’s why PHP constants are a very good alternative for configuration settings.

If you want a read that really goes in-depth about global variables and so forth, you can check out “Why Global Variables in PHP is Bad Programming Practice“


Posted in PHP, Uncategorized | View Comments

blog comments powered by Disqus

 
Wordpress Themes by and Website Templates by Blogcut Blogged Blog Directory Blog Directory - Blogged