RE: [PHP] globals in functions

From: Date: Sat, 13 Apr 2002 17:58:58 +0000
Subject: RE: [PHP] globals in functions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-92891@lists.php.net to get a copy of this message
1: Globals are bad...m'kay. You should never use globals. If your function needs a variable, you should pass it in. There are exceptions to this rule but it's not a good idea to program normally this way. 2: Is this a form? It sounds (from the way you word it) that the variables are part of a form. If so, pass $_POST into your form and it will be able to evaluate them. If it's METHOD=GET then use $_GET. This is also a much more generic way to program since if you add a new variable to the form, you don't have to make it global in this function. if (myFunction($_GET)){ echo "Everything is hunky dory!"; } else { echo "Blow Chow"; } function myFunction($formArray=null){ if (isNull($formArray){ return false; } if (isarray($formArray)){ return true; } else { return false; } } // function myFunction($formArray=null) WARNING: I have not tried the code above. Use at your own risk. But the concepts are there. * * Cal Evans * Journeyman Programmer * Techno-Mage * http://www.calevans.com * -----Original Message----- From: Paul Roberts [mailto:roberts_paul@bigfoot.com] Sent: Saturday, April 13, 2002 12:37 PM To: php-general@lists.php.net Subject: [PHP] globals in functions Is there a quick way to set all variables as global so that they are avalible to a function, i'm doing an eval inside, so i need all the submitted variables to be avalible, or do i have to decalre them individualy. Paul Roberts roberts_paul@bigfoot.com ++++++++++++++++++++++++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#92891) next »