SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Mo Money's Avatar
    Join Date
    Nov 2002
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy globals? register_globals? help!!

    I am getting better at php/mysql but there is infinitely more to learn. I dont really know much at all about globals/register_globals? can someone explain more about them to me?

    One thing that confused me recently was I maid a function:

    simplified example:
    functions.php page:
    PHP Code:
    <?php

    function mo () {
    $mo "apple";
    }

    ?>
    but then when I tried to call mo(); then $mo I couldnt access the $mo var.

    PHP Code:
    <?php

    require("functions.php");
    mo();
    echo 
    "$mo";

    ?>
    So I asked my PHP teacher what the problem was and he told me to make $mo a global or something, can you guys clarify for me? Thank you!
    AbcArcade.com - free internet games!

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mo

    If you want to make a variable global, it needs to exist outside the function, and to refer to a global variable inside a function you need to declare it as global inside the function (best done right at the start).

    So:
    PHP Code:
    <?php
    $mo
    # global variable

    function mo () { 
    $mo "apple"# local version of $mo inside this function
    # independent of global $mo

     
    function 
    mo2 () { 
    global 
    $mo # global version of $mo inside this function
    $mo "apple"


    ?>
    Hopefully that illustrates it. In case something I said wasn't exactly correct, I think the PHP manual has a discussion about variable scope.
    Paul Davey
    webmaster for Whitford Church of Christ

  3. #3
    SitePoint Zealot Egghead's Avatar
    Join Date
    Feb 2002
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yo Mo,

    There's loads of stuff on register globals in this forum already, but basically you need to know that by default, the setting for register globals is now turned off meaning you have to access variables used in different scripts by calling them through the superglobals. It's a security thing and it's worth keeping - the practice of accessing your variables through $_POST['var_one'], $_GET['var_one'] or whatever instead of $var_one soon becomes natural.

    As for accessing your variables in a function, you want to learn about the scope of variables or 'from which part of your script variables can be accessed from'...

    If you create and use variables in a function that you have made in your script, then those variables will only be accessible within that particular function. (These are known as local variables with local scope)

    Likewise a variable defined outside of a function can not be used inside the function, unless it is referred to the function (passed as an arguement when the function is called) or it is a global variable.

    Global variables can be quite wicked to use because of problems of overwriting the value within different functions. Just be careful when you use them.

    You may also want to check out static variables and defined constants too.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Quote Originally Posted by Mo Money
    [...]

    So I asked my PHP teacher what the problem was and he told me to make $mo a global or something, can you guys clarify for me? Thank you!
    Bad teacher for advising to use "global"
    I have recently realized what problems using global can cause when I helped someone with his code.

    The better solution is to use parameters and return the value

    PHP Code:
    <?php

    function mo($value) {
       
    $value 'apple';
       return 
    $value;
    }
    ?>
    Another option is to pass the parameter by reference using "&" instead by value.

    PHP Code:
    <?php

    function mo(&$value) {
       
    $value 'apple';
    }

    ?>
    Now, every variable you pass to the function mo will be assigned the string 'apple':

    PHP Code:
    <?php

    $a 
    'a';

    function 
    mo(&$value) {
       
    $value 'apple';
    }

    mo($a);

    echo 
    $a// outputs: apple

    ?>


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
  •