October 11, 2006
Experts Exchange Community News
You are receiving this because you are an Experts-Exchange.com member who has opted-in to receive newsletters.
What's New at Experts Exchange
Click here to unsubscribe from the newsletter.
Global variables in PHP: a solution

dspector is a longtime Web and Windows programmer. Besides doing software engineering, his hobbies include working for civil rights for blind and visually impaired people, teaching deep meditation, playing Go, and contributing to Wikipedia.

Global variables are a fact of life. Although global variables are technically never necessary, particularly in PHP programs that use object-oriented design, they often make programs easier to read and easier to write. There are definitely situations in which they are reasonable and appropriate: resources or handles returned from API calls that are used in many functions, frequently-used arguments passed in via GET or POST, and very long strings used in cryptological protocols are some cases that come to mind.

The problem I have with global variables in PHP is that they work the opposite way they do in other languages: they are declared in each function, instead of at file level. This is about as verbose and difficult to maintain as passing each global variable used by a function in its argument list. Argument lists (or "global" statements) become longer, and must change wherever needed when a new global variable is added to the program.

I've spent hours debugging problems caused by omitting a variable from a "global" statement. I consider it a crime to have robbed me of my time like this. And no, I can't make do by using the special $GLOBALS array. That syntax is even worse, because it puts the syntactic burden on every reference to a global variable. The syntactic burden should only be in one place: at a single declaration of the global variable at file level, as it is in most other programming languages.

Here is my solution: a way in PHP to declare global variables at file level and have them automatically be available inside functions, using a modicum of elegance (although this method does incur the expense of importing all declared global variables into each function, whether they are referenced or not). Here is the usage, given by example:

// File level: declare global variable $MySQL,
// the MySQL connection resource
G("MySQL");
 
// A typical function
function DoSQLQuery($Query)
   {
   eval(G); // Import all global variables
   ...
   // Variable $MySQL can be referenced here
   ...
   } // DoSQLQuery

If you want the global declaration to include an initial value, you can write it like this:

G("MySQL",""); // Init to empty string
G("gModulus","a37406fb43..."); // Init to string
G("base",2); // Init to number

Here's all the code you need to implement this technique; include this code at the start of each PHP file:

$GLOBALS["G_"]='$_';
define("G",'eval(eval(\'return \\\'global \\'.$GLOBALS["G_"].\\\';\\\';\'));');
function G($VarName,$Init=NULL)
   {
   $GLOBALS["G_"].=",\$$VarName";
   if (!is_null($Init))
      $GLOBALS[$VarName]=$Init;
   } // G
The Premium Services include a number of features not available to "limited" members. Among them:
> Unlimited question points
> VIP Search
> Bookmarks
> Quick Links
> Collapsible menu
> No ads
You can purchase Premium Services on a month-to-month, semi-annual or annual basis, and take full advantage of all that Experts Exchange has to offer!
Solitaire on steroids

Netminder is one of the Site Administrators at Experts Exchange.

We'll be the first to admit it: we don't do games much. Except for the ones that came with the operating system, our experience with them is limited to kibbitzing over our better half's shoulder once in a while -- and even then, we don't claim to know much about what's going on. We did once fiddle around with Myst, and a four-year-old of our acquaintance was a whiz when it came to playing Doom -- but for whatever reason, we never got into them much.

But three stories caught our eye last week, so we thought we would pass them along, if only because we know we have not given the Gamers their due. We caught some flak back in January when we announced the Expert of the Year awards because the Game Emulators topic area was included. Memo to Experts: Game Emulators was 32nd on the list of TAs by page views, right below C++ Programming, and right above Microsoft Excel.

The first story says that the Quad Core computers will be useless for games. The main problem, developers say, is that the performance isn't very scalable. That's something the database and programming folks have been dealing forever, but for the gaming developers, the notion of introducing more cores is also a red flag because it means more bugs. Since the market for games is dependent on new products fairly frequently (people can only figure out the tricks and win the game so many times before they are bored), optimizing for four cores will hurt, rather than help.

The second story is about everyone's favorite whipping boy: Microsoft's Vista, and in this particular instance, it is Microsoft that is delivering the bad news: Games will run ten to fifteen per cent slower using Vista, compared to Windows XP. The rap is on the 3-D desktop API -- which Microsoft has been telling developers will work up to four times faster.

Finally -- and this is only marginally related -- PCPro/UK has found the fastest PC on the market: the Apple Mac Pro with the Quad Core Intel processor. Now, before all you Mac fans start needling all of us who use PCs, that's the good news. The bad news: you have to dump the OS X operating system and use Windows XP. Still, given the price tag of the basic model, it's worth a second look.

Especially if you plan to buy new Desperate Housewives game as an early Christmas present to yourself.

It's not all about you

ericpete is the editor of the Experts Exchange newsletter. He considers the main benefit to doing that job to be the opportunity to write -- something he has done professionally since he was a young boy.

My father is one of my favorite writers. He spent most of his early life shaping his craft before he finally bought a couple of small newspapers where he could write what he wanted without worrying about what his boss might think. He was passionate without being strident, compelling and not overbearing, witty without stretching to make a joke. But his most remarkable attribute, as a reporter, was in his ability to get to the heart of a story in very few words.

We didn't get a lot of advice from him in advance, which was probably a good thing; the surest way to get children to do things is to tell them not to do them, right? So his counsel was generally limited to brief comments after the fact, when they would have the most impact.

One such piece of advice: Never get into a pissing contest with a skunk.

So what does this have to do with the digital world we live in? Well, a few weeks ago we happened to pick up the Kansas City Star's sports section. In it, there was a column written by Jason Whitlock, in which he called out one of his colleagues, Scoop Jackson of ESPN. A decade or so ago, newspaper columnists didn't do that kind of thing; they might disagree, but they would worry about their own point of view, and making their own case.

Not that we mind the ongoing war of words; there's something deliciously gratifying about watching two oversized egos going at it like seven-year-olds on a playground. In the days -- when one had to pick up a newspaper and get one's fingers a little discolored -- we never would have been able to read the literate but pretentious Whitlock, and wouldn't have bothered with the self-serving but relevant Jackson. It's almost as entertaining as hearing about Paris Hilton's cell-phone.

Another piece of advise from Pop: Report the story, don't become the story.

But that's what the Internet has done, although there's no question that the money and exposure of television set the table. Forget all the angst about "Web 2.0" -- mass communication changed fifty years ago, when a lot of newspapers switched to offset printing. Bigger newspapers had an expensive piece of equipment on which they wanted a return on investment (a press), and smaller newspapers couldn't justify the expense of equipment that ran for only a few hours a week -- and all of a sudden, anyone with a carbon ribbon typewriter and a camera could start his own paper.

The Internet has taken it one step further. Anyone can have a website, anyone can set up a blog, and anyone can have his say. What we fear, though, is that if everyone is talking, it means that no one is listening. Freedom of speech -- the right to say what you want about your political beliefs, or about your religious beliefs -- is not the same as the right to be heard, and it certainly doesn't mean that you have anything valuable to say. "Everybody's talkin' at me," Fred Neil wrote, "I don't hear a word they're saying."

And it's not just your everyday blogger -- even the world's self-proclaimed "most respected name in journalism" is susceptible: the lead story the day Katie Couric took over the anchor desk for CBS News was Katie Couric taking over the anchor desk at CBS News. But that's what we have done to ourselves; our little corner of the universe is now the center of it.

Tips from the Moderators
Someone gave me a C grade, but my answer is correct. Can I do something about it?
Yes. Post a request for a review in the Community Support topic area. Generally speaking, the C grade should be given only when the answer is marginal at best -- after all, it is the lowest possible grade you can give. You should always give the Experts a chance to improve the grade by providing more information. If you have given someone a lower grade, but want to improve it, Community Support is the right place to make your request as well.
I have a question, but I'm not sure where to ask it. Is that what the New Topics TA is for?
No. The New Topics TA is for suggesting new topic areas at Experts Exchange. If you're not sure where to ask it, ask a question in the Community Support topic area, and the Moderators will give you a few pointers. You can also look at the All Topics page.
What do I do if I have a complaint?
To a certain extent, that depends on what kind of complaint it is. Your first stop should be the Community Support topic area; the Moderators are trained to arbitrate disputes between members, and have the resources to resolve most problems. If your issue is with a Moderator, your next step is to write to the Administrators; the Site Admins are listed on the help page.
Page Two: More News and Notes
Nata's Corner: A quick look at Vista

woman in specticalsI got an email the other day from someone asking me if they should upgrade to Vista when it comes out. Now, I don't claim to know all the ins and outs of Vista, but I will tell you this: I don't like upgrading just because. My computer has Windows XP on it, but that's because my other half bought it for me early this year, and that's what came on it; my old one was dying, but I was perfectly happy with Windows98.

So with that in mind, here's what I can tell you about Vista. The interface reminds me more of a Mac than it does Windows XP. It's very futuristic, and very slick. Along with that, Microsoft is completely reworking Internet Explorer, Windows Movie Maker and Media Center. But there are a few downsides too. For one, you need to have a fairly new computer if you plan on upgrading, because Vista's hardware requirements are pretty steep. Second, at this point, Microsoft wants you to use their security system, which, if you have been reading this column for a while, you know is... well ... silly. Personally, I want at least two different programs monitoring my computer, because with all the spyware, viruses and stuff going around, no one program ever seems to get them all. At the very least, I want a choice.

I expect that EE will be adding a Windows Vista topic area pretty soon, and most of the Experts at EE know a lot more about it than I do, so be patient -- it will be there. But I will tell you this: I probably won't move to Vista until I absolutely have to.

The newsletter had an item a while back about how Yahoo was coming out with a beta version of its email that had an Outlook Express type of interface. Now Hotmail is doing the same kind of thing. It has a preview pane and autocomplete for addresses in your address book, and offers 2 Gb of storage.

Sophos -- my favorite site for information about threats, by the way -- has a new paper out on what is the biggest threat, in my book, to your computer: having it become a zombie. The paper has a really good description of how they are created, how they have evolved, and what you can do about them. The last numbers I saw suggested that something like 5 million computers are being controlled by bots, so it's serious. If you're interested, Symantec's report on security threats in the first half of 2006 is also available.

Inside the numbers
ameba, one of EE's prominent Experts, provides us with a list of newly earned Certificates. His list of all of the Certified Experts is located at his site. The list below covers the period from September 25 through October 9.
Expert Certified in Topic Area
mcallarse Nightman rw3admin LandyJ dduser Hypnochu srafi78 raj_ Mark_FreeSoftware jkaios YZlat dbaduck ashutosh9910 skvikram Chinmay_Patel jatinderalagh ryancys VirusMinus giltjr pjtemplin war1 dakyd bpmurray Justin_W joechina Raynard7 redseatechnologies emoreau TimCottee gangwisch Ready1 SystmProg Chris-Dent Guru Master Master Master Master Master Master Master Guru Master Wizard Guru Master Master Master Master Wizard Master Wizard Master Genius Sage Master Guru Master Master Sage Sage Guru Master Master Sage Wizard MS Access Microsoft SQL Microsoft SQL Microsoft SQL Microsoft SQL Microsoft SQL Microsoft SQL Microsoft SQL Visual Basic Visual Basic ASP.NET ASP.NET ASP.NET ASP.NET ASP.NET ASP.NET ASP ASP Networking Networking Windows XP JavaScript JavaScript C# C# C# Exchange_Server VB.NET VB.NET VB.NET VB.NET Win. Server 2003 Win. Server 2003
Expert Certified in Topic Area
jebeckham Abs_jaipur plimpias Cherukuri30 humeniuk OliWarner b0lsc0tt TimCottee dij8 rllibby TName sujith80 jwahl ShaneDevenshire david251 spattewar SamIDRC ozo sunnycoder usachrisk1983 SysExpert peter57r TimCottee matthewspatrick wstuph mark-wa kjanicke lupuleine TeRReF fibo rindi kblack05 cj_1969 Master Master Master Master Master Wizard Master Master Master Genius Master Master Master Wizard Guru Master Master Master Master Guru Master Wizard Master Master Master Wizard Master Master Master Master Wizard Master Guru Win. Server 2003 Win. Server 2003 Win. Server 2003 Win. Server 2003 Win. Server 2003 Web Development Web Development Web Development Web Development Delphi Delphi Oracle Oracle Excel Excel Excel Excel C++ Operating Systems ColdFusion Microsoft Network Databases Databases Databases Databases Outlook Outlook Flash PHP and Databases PHP and Databases Linux Linux Applications
Expert Certified in Topic Area
sunnycoder dann47 pgm554 Raynard7 virmaior lokus amit_g teraplane VirusMinus callrs Adam314 rindi callrs rsivanandan billwharton Dave_Dietz chris_calabrese ravenpl chris_calabrese noci mgh_mgharish sunnycoder amit_g JoseParrot BongSoo TechSoEasy cnagaraj25 murphey2 Saineolai mgcIT public wyliecoyoteuk Master Master Master Guru Guru Guru Master Master Guru Master Wizard Master Guru Guru Master Genius Master Master Master Master Guru Master Master Master Master Guru Master Master Master Wizard Master Master Applications Applications Storage Mysql Mysql Mysql Mysql Mysql CSS CSS Perl Windows Security Miscellaneous Routers Firewalls IIS Solaris Unix Prog. Unix Prog. Linux Net. Math & Science Unix Unix Graphics Graphics Sharepoint Sharepoint AS/400 SBS Small Bus. Server Citrix DSL/Cable HP Printers
2363 experts have 4003 certifications: Genius: 117 Sage: 175 Wizard: 266 Guru: 709 Master: 2736
Copyright ? 2006. All rights reserved.