If I reference a variable external to a function using "global", what happens if the variable doesn't exist?

Example:

// $discount does not exist

function doThing()
{
   global $discount;

   if (!empty($discount))
   {
      // do stuff
   }
}

Nothing seems to break, but is it bad practice? Is there a better way to check if a variable exists outside a function?

  • Bob

    That should be the way to do that. That said, it would be good to prefer a function parameter to a global variable.

      Or you could check if the variable exists via the GLOBALS superglobal array:

      // $discount does not exist 
      
      function doThing() 
      { 
         if (array_key_exists($GLOBALS['discount']))
         {
            global $discount;
      
        // do stuff
         } 
      }

      Although as suggested, variables from the global scope should be passed by reference as an argument, rather than being accessed via the local scope.

      // better
      function doThing($discount) 
      {
            // do stuff 
      }
      
      doThing(&$discount);

        Actually, pcthug, that syntax is now deprecated. If you want to pass by reference, you have to change the formal parameter, not the argument passed.

          Touché.

          When did such deprecation take place? Enlighten me.

            Sorry, but I do not remember exactly when that happened.

              If I'm just reading the variable, and not trying to modify it in the function, I probably don't want to pass it by reference, right?

              As far as passing it as a function parameter, I guess I should add that this function is inside a class. I guess I could pass another parameter via the class constructor, but then I will have to define it outside the class even if it's zero or null. I define this variable in a configuration includes page, and I'd like the class to not break if it's not defined.

              In plain English, what the class says is "now that you've done a bunch of work, go see if there is a standard discount percentage set up; if so, get it and apply the discount; otherwise, just return the non-discounted amount".

              If that helps.

                laserlight wrote:

                Sorry, but I do not remember exactly when that happened.

                Since at least PHP 4.0.0. Possibly (though probably not) earlier.
                From a version of php.ini dated April 7, 2000:

                allow_call_time_pass_reference = Off

                ; whether to enable the ability to force arguments to be
                ; passed by reference at function-call time. This method
                ; is deprecated, and is likely to be unsupported in future
                ; versions of PHP/Zend. The encouraged method of specifying
                ; which arguments should be passed by reference is in the
                ; function declaration. You're encouraged to try and
                ; turn this option Off, and make sure your scripts work
                ; properly with it, to ensure they will work with future
                ; versions of the language (you will receive a warning
                ; each time you use this feature, and the argument will
                ; be passed by value instead of by reference).

                  Write a Reply...