Results 1 to 3 of 3

Thread: Limited use of "global" variable

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default Limited use of "global" variable

    I'm creating a function that I want to "import" a variable from global scope and I want to modify this variable. I do NOT want to save these modifications to the global variable, but only within the function.


    The only way I know to do this is to copy it manually within the function, but this seems like a waste of system resources (note that in the actual code I'm using "$var" is a huge array).
    PHP Code:
    function myfunc() {
       global 
    $var;
       
    $varcopy $var;
       
    $varcopy++;

    What I'd really like is a keyword instead of global like from_global_as_copy.
    Any ideas?

    This isn't crucial (my function is working fine), but I would like, at some point, to optimize the code.



    EDIT: I seem to have found my answer. Instead of using global $var; I should use:
    $var = $GLOBALS['var'];

    Does anyone have any other thoughts on this?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    What exactly were you trying to optimize? And if your code was working fine without the $_GLOBALS superglobal, why did you need it. Enlighten us.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's part of a complex system, more than is worth trying to explain here.

    The situation is this:
    1. I have a very big array of data. Let's call it "cars".
    2. I want to have a function called searchcars().
    3. Within that, I want to search through $cars and find the "good" ones (or whatever).
    4. I want to only keep the "good" ones and remove the others.
    5. Then I want to do other things within that function like display them, save them, email them, etc.
    6. I want to NOT change the global value of the array $cars-- just inside the function.


    In other words, I want to get the value of global $cars, but I do not want to link the local $cars to the global $cars.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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