Henry Lyne (henrylyne) wrote,
Henry Lyne

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

  • LJ Code

    I just discovered that the LiveJournal code is no longer open source. http://code.livejournal.org/ returns a blank page. That sucks. I wish I had a…

  • Chrome and Keychain Access

    Dear Lazyweb, I don't want to give Chrome access to Keychain on my Mac OS X Lion laptop, but every time I start Chrome I am asked again to grant…

  • Number of day to Belize

    Three days before I visit Belize again. I was looking through my old LJ posts to see if I had a photo of the white board I used for Beize…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic