WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Nice register globobals - style function
Categories : PHP, Global Variables Click here to Update Your Picture
Justin French
Date : May 04th 2003
Grade : 4 of 5 (graded 5 times)
Viewed : 4260
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Justin French
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Do you like the security of register_globals being off, but hate referring to the variables in the
superglobal arrays? if so, you probably do something like:
<?
//a
if(isset($_GET['foo'])) { $foo = $_GET['foo']];
?>

Or worse still:
<?
//b
$foo = $_GET['foo'];
?>

... just so that you can use $foo instead of $_GET['foo'].

The first one is quite labour instensive, and the second *works*, but will cause PHP to spit out a
warning message with some error reporting settings.

I wrote a quick function to pull all get (in this case), post, cookie, session, etc variables in as
regular $foo variables.

<?
//c
function getVars()
        {
        $vars = func_get_args();
        foreach($vars as $k => $v)
                {
                if(isset($_GET[$v])) { $GLOBALS[$v] = $_GET[$v]; }
                }
        }
?>

Usage:
<?
//d
getVars('id','page','action');
?>

This will look for $_GET['id'], $_GET['page'], $_GET['action'], and if they are set, the
corresponding $id, $page and $action will be set.

Other than cutting down on lines of code as per the code snippet "a" above, and preventing the
errors that may happen with code snippet "b", it's easy to quickly see which variables you're
expecting on each page, and for scripts which SOMETIMES expect some variables, the if(isset(...))
part of the function will make sure it's all error/warning free.



using global variables
Categories : Global Variables, PHP
filter untrusted GET and POST variables and create trusted variable of same name
Categories : PHP, Global Variables, Security
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
How to make sure a that $foo is from a cookie and not from the URI.
Categories : PHP, Variables, Global Variables, Cookies
$REMOTE_HOST does not return a value.
Categories : PHP, Global Variables, HTTP
Initialize global variables for every field in a table. This version requires that phplib is installed on your server.
Categories : Global Variables, MySQL, PHP, Variables
pick up an array of variables from a query string such as: http://www.archipro.com/test.php?state=AB&state=BC
Categories : PHP, Strings, URLs, Global Variables
forum field names
Categories : Global Variables, PHP, PHP Configuration, Installation
Make old style (PHP3) scripts using GET, POST, COOKIE and File uploads (POST) compatible with PHP 4.2.0
Categories : PHP, HTML and PHP, Global Variables, Cookies, Variables
Global Dump Highlighted
Categories : PHP, Variables, Global Variables
Simple script to passing persistent and growing array between recalls of one page (manipulate little stack).
Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables
How can I know about what Operating System is running on server from PHP?
Categories : PHP, Global Variables
getting the name of the current script and query string
Categories : PHP, Global Variables, Variables, URLs
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
 matthew waygood wrote : 956
Not a bad example, but loved the use of func_get_args, I will be looking at where I can use this from now on.
 
 Max Sullivan wrote :963
extract($_REQUEST, EXTR_IF_EXISTS); works too and it uses built in php functions, rather than including the above with your code.  The extract function is also easier becaues you don`t need to specify each variable you want.