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 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2006
    Location
    north poll
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    variable in a function

    i have a function and i have it running on a page, but i set a value for a variable in the function and i want the variable to be able to be used for the page not just in the function. how do i do this? thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Difficult to explain because there are many aspects to this, but this page tells it all:

    http://www.php.net/manual/en/language.functions.php

    See the part about returning values.
    http://www.php.net/manual/en/functio...ing-values.php


    ... and you'll learn a lot more too.

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I also think taking a look at variable scope will help you as well:
    http://us3.php.net/global

    good luck.

  • #4
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The short answer to that particular scenario (if I understand it correctly) is to "return" the variable.

    Is the value of that one variable the result of the function or just one small piece of what it does?
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #5
    New Coder
    Join Date
    Oct 2006
    Location
    north poll
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my function is

    <?php
    function user_level()
    {
    if ($_SESSION['lvl']
    {
    $lvl = $_SESSION['lvl'];
    }
    else
    {
    $lvl = "user"
    }
    }
    ?>

    and now i want to use the $lvl variable to show the lvl of each user for my forum. i dont see how the return will hlp

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    function user_level() {
      if (isset(
    $_SESSION['lvl'])) {
        
    $lvl $_SESSION['lvl'];
      }
      else {
        
    $lvl "user"
      
    }
      return 
    $lvl;
    }
    $level user_level();
    echo 
    $level
    Something like that?

  • #7
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's what I'm screamin'!
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)


  •  

    Posting Permissions

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