Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2005, 10:08 AM
Authorized User
 
Join Date: Dec 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is it better to pass variables to a function?

I've read on lots of sites that register_globals in the php ini should be turned off and that instead of using

global variableName;

in functions to access variables you should pass variables to functions as arguments.

Is this faster? Better? Safer? If so, why?

Some of my functions need access to loads of variables, I find it easier to just use 'gloabls variableName;'. Should I try and convert my code to used passed arguments? These could be passed by reference, so that the variables outside functions could be updated. Wouldn't this be pretty much like just using a variable ina function with the global prefix before it?
 
Old October 19th, 2005, 01:38 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Bhopal, MP, India.
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

global variables are a bad idea.
Pass variables to a function; which makes code modular.
Still better if you wite class definition, instantiate an object and they call function on it (object).

`~@#\^%&*/\.<.\/-|+|_!:;..=?>
Support Indian students' finances http://scholarship.mediasworks.com/
 
Old November 3rd, 2005, 10:15 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if register_globals has exploits and you can do it easily without doing things that way. Why increase the risk of your application by using register_globals? There are plenty of exploits to worry about why create one more? Its cleaner, better, safer code to pass variables to a function. Although the wrox basic php book uses register_globals in it's examples please grow past the shortcomings of this book.....

 
Old November 4th, 2005, 05:07 AM
Authorized User
 
Join Date: Dec 2004
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Some of my functions have lots of variables currently accessed through the keyword global.

e.g.
Code:
    function loginForm($errorMessage = "no_error")
    {
        global $self;
        global $loginMessage;
        global $loginPassword;
        global $loginUsername;
        global $loginKeepLogged;
        global $loginSubmit;
        global $loginReset;
        global $loginLanguage;
        global $loginDefaultLang;
        global $loginTheme;
        global $loginDefaultTheme;

        ...rest of function
    }


I think maybe I can put all these variables by reference into an array (using a new function) and just pass the array by reference to the function. Would this work? A variable passed by reference twice, would the original still get upadated?




Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Variables obrienkev C# 2005 1 July 18th, 2008 03:58 AM
How do I Pass Session Variables cdanczak Classic ASP Professional 0 November 27th, 2007 10:42 AM
how can I pass variables to vb? astarti Beginning VB 6 4 September 5th, 2007 08:43 AM
Can't pass variables to form for validation! gnilly PHP How-To 4 April 24th, 2005 06:33 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.