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
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post

    Thumbs up variable problems in functions (SOLVED)

    Hello
    I'm building a news script and I got problem:
    I have code similar to this:

    index.php
    PHP Code:
    blablabla
    <?php
    require('includes/settings.php');
    news();
    blablablabla
    ?>
    in settings.php I put some variables and include 2 files...
    and variables aren't set inside the function, for example, i have in news() such piece of code:
    PHP Code:
    echo $base
    it won't output what it should do and when I check it with isset(), it says, that var isn't set, but when I put echo $base outside the function, it works with no problem.
    help!
    Last edited by M.W.S.; 01-20-2008 at 04:34 PM.

  • #2
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post
    you have to pass variables to the function or make them global

  • Users who have thanked vinternet for this post:

    M.W.S. (01-20-2008)

  • #3
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    could you explain how?

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Each function has it's own "scope" for variables, so if they aren't global or superglobal, they have to be passed in the function call:
    PHP Code:

    function news($var)
    {
        echo 
    $var;
    }

    news($base); 
    I used "var" as an example to avoid confusion, you could use "base". More info: Manual

  • Users who have thanked Inigoesdr for this post:

    M.W.S. (01-20-2008)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Global can be completely avoided and IMO shouldn't be used; parameter passing is really the best control you can use.
    Course, the old HTTP_*_VARS did require globalization, but unless you are supporting an obsolescent version of PHP, I wouldn't worry too much about the long arrays.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks everyone, I did it


  •  

    Posting Permissions

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