Including a function, and the variable created within cannot be found

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keiron Waites

    #1

    Including a function, and the variable created within cannot be found

    Hi,

    I include the following function:

    <?php
    function login($members_ only) {
    if (isset($_COOKIE['login'])) {
    $login = explode("|split |",$_COOKIE['login']);
    $login = array("username "=>$login[0],"password"=>$l ogin[1]);
    }
    else if ($members_only == 1) {
    header("Locatio n: login.php");
    }
    }
    ?>

    in a file called account.php:

    <?php
    require("websit e-includes/login.inc.php") ;
    login(1);
    [snip]
    ?>

    The problem is that I need to be able to access $login outside of the
    function within the page that calls it. Is this possible? I'm new to
    functions and before I was just including code, but I am told functions are
    better.

    TIA


  • Jochen Daum

    #2
    Re: Including a function, and the variable created within cannot be found

    Hi Keiron!

    On Thu, 11 Sep 2003 20:12:55 +0000 (UTC), "Keiron Waites"
    <webmaster@-NOSPAM-sharemonkey.com > wrote:
    [color=blue]
    >Hi,
    >
    >I include the following function:
    >
    ><?php
    >function login($members_ only) {
    > if (isset($_COOKIE['login'])) {
    > $login = explode("|split |",$_COOKIE['login']);
    > $login = array("username "=>$login[0],"password"=>$l ogin[1]);
    > }
    > else if ($members_only == 1) {
    > header("Locatio n: login.php");
    > }
    >}
    >?>
    >
    >in a file called account.php:
    >
    ><?php
    >require("websi te-includes/login.inc.php") ;
    >login(1);
    >[snip]
    >?>
    >
    >The problem is that I need to be able to access $login outside of the
    >function within the page that calls it. Is this possible? I'm new to
    >functions and before I was just including code, but I am told functions are
    >better.[/color]

    Have a look at variable scopes in function on www.php.net, but better
    look at functions and use return.

    Jochen


    [color=blue]
    >
    >TIA
    >[/color]

    --
    Jochen Daum - CANS Ltd.
    PHP DB Edit Toolkit -- PHP scripts for building
    database editing interfaces.
    http://sourceforge.net/projects/phpdbedittk/

    Comment

    • Alex Farran

      #3
      Re: Including a function, and the variable created within cannot befound

      Keiron Waites writes:
      [color=blue]
      > Hi,
      > I include the following function:[/color]
      [color=blue]
      > <?php
      > function login($members_ only) {
      > if (isset($_COOKIE['login'])) {
      > $login = explode("|split |",$_COOKIE['login']);
      > $login = array("username "=>$login[0],"password"=>$l ogin[1]);
      > }
      > else if ($members_only == 1) {
      > header("Locatio n: login.php");
      > }
      > }
      > ?>[/color]
      [color=blue]
      > in a file called account.php:[/color]
      [color=blue]
      > <?php
      > require("websit e-includes/login.inc.php") ;
      > login(1);
      > [snip]
      > ?>[/color]
      [color=blue]
      > The problem is that I need to be able to access $login outside of the
      > function within the page that calls it. Is this possible? I'm new to
      > functions and before I was just including code, but I am told functions are
      > better.[/color]

      At the end of your function you could add :
      return $login;
      and call it with:
      $login = login(1);

      Another thing you could is use objects to define a scope in which
      functions can share variables. Like this:

      <?php
      class login_class {
      // Shared variables used by login functions.
      var $login;
      var $members_only;

      // Constructor function that intialises the object.
      function login_class( $members_only ) {
      $this->members_only = $members_only;
      }

      // The login function now refers to class variables.
      function login() {
      if (isset($_COOKIE['login'])) {
      $this->login = explode("|split |",$_COOKIE['login']);
      $this->login = array("username "=>$this->login[0],
      "password"=>$th is->login[1]);
      }
      else if ($this->members_only == 1) {
      header("Locatio n: login.php");
      }
      }

      // Another function that uses $this->login
      function get_username() {
      return $this->login("usernam e");
      }
      }

      ?>


      <?php
      require("websit e-includes/login_class.inc .php");
      $login_class = new login_class(1); // Instantiate and initialise your class.

      $login_class->login(); // Call a function.

      ?>

      --

      __o Alex Farran
      _`\<,_ Analyst / Programmer
      (_)/ (_) www.alexfarran.com

      Comment

      Working...