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

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic