Henry Lyne (henrylyne) wrote,
Henry Lyne
henrylyne

PHP Scope

Working on a PHP script for someone tonight I banged my head on the most unusual problem. I was trying to access what I thought was a globally declared array from within a function. I kept getting the error Undefined variable: foo when trying to use the foo array. I tried passing the array into the function, and that worked, but seemed unnecessary.

It turns out that you need to declare global variables inside functions that want to use them. So by putting global foo; inside my function it could now access the globally declared array. I don't think I've ever come across this method for handling global variables before.
Tags: hacking, php
Subscribe

  • Bike to Work Day

    I normally get annoyed with cyclists on bike to work day, but today wasn't too bad. My main peeve in the past was the SF Bicycle Coalition "energizer…

  • Traffic Violation

    I got busted running a stop sign at Octavia and Market this morning. A CHP motorcycle cop pulled me over and seemed a little apologetic about having…

  • Kung Fu Tournament

    I'm competing this weekend at a kung fu tournament in San Francisco. I'm competing in the chi sao division along with some of the guys from the…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic
  • 0 comments