Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts

    convert $_GET variables to regular variables? about 10 times

    Do you guys always convert your GET and/or POST variables to regular variables
    PHP Code:
    $variable foo
    ? On a page, I'm using a GET variable about 10 times, would it be more efficient to convert it to a regular variable (I'm sure there's a name for this type of variable) at the beginning? Does it have anything to do with load on the PHP (interpreter, like it goes to check the variable from the URL every time, which is maybe more of a strain than from the document itself), or just mere file size?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I just think its easier to type a variable name rather than $_GET['var'];

    The strain on the parser is the same whether you assign it to a variable or not. Also it helps for preventing mysql injection. Declaring the variables to empty to start with and then checking the data to see if its valid and then reassigning it the variable. I don't think you can do this
    PHP Code:
    $_GET['var'] = ''
    I could be wrong though, don't know.

    Edit: Guess you can do that though again, it takes a little more work to type e.g. SHIFT + 4 _ G E T [ ' foo ' ], compared to a variable called foo.
    Code:
    $foo = $_GET['foo'];
    PHP Code:
    if(isset($foo)) echo 'Foo is set!'
    Last edited by _Aerospace_Eng_; 07-27-2007 at 09:37 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder sdcomputerz's Avatar
    Join Date
    Feb 2007
    Location
    Hawaii
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I second that
    You can never have nothing, because nothing is still something.
    SD Computerz
    Free Domain Names
    Gaming Lagoon - Buying your game time!

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    I prefer to leave variables in their original form unless there is an overriding need to alter it (or I alter the variable via script) , there is no protection against injection unless register_globals is on and variables are not declared locally before they are used (or filtered if they are GPC)

    From a resource viewpoint there is an (probably too small to measure) overhead in declaring the variable twice (even if you then unset the original) so if resource usage worries you then don't do it.

    It is easier on the typing though
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 Posts
    For variables that might or might not exist (from a form, a url, a cookie, a session...) I always do something like this near the beginning of the code -
    PHP Code:
    $get_action = isset($_GET['action']) ? $_GET['action'] : ''
    The variable is then only checked if it exists once and is evaluated only once. The internal variable will then exist through out the remainder of the code with either the actual input or an empty string.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    I don't understand making it empty first, why does that help?
    I have example.com/?var=foo, and let's say I want to echo $var on that page (plain text, all that is on the page is foo). How would you do that?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •