Using variables in Two Function?

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

    Using variables in Two Function?

    I have a page that has two functions on it, on load I check for posted
    value if exists I set some variables that I want to use in the
    functions but don't want to pass back and forth. Do I need to be using
    global variables in order to use this variable in each function? Are
    global variables only available to the page they are called correct?
    Never used them before but I'm not getting the values if I just set a
    $var at the top of the page. Thanks

  • milahu

    #2
    Re: Using variables in Two Function?

    You can either use globals (a) or pass a variable pointer to the
    function (b).

    a)
    $foo = true;
    function f($a, $b) {
    global $foo;
    $foo = false;
    }
    f(1, 2);

    b)
    $foo = true;
    function f($a, $b, &$global) {
    $global = false;
    }
    f(1, 2, $foo);

    Cheers

    Comment

    • Jerry Stuckle

      #3
      Re: Using variables in Two Function?

      Barkster wrote:[color=blue]
      > I have a page that has two functions on it, on load I check for posted
      > value if exists I set some variables that I want to use in the
      > functions but don't want to pass back and forth. Do I need to be using
      > global variables in order to use this variable in each function? Are
      > global variables only available to the page they are called correct?
      > Never used them before but I'm not getting the values if I just set a
      > $var at the top of the page. Thanks
      >[/color]

      Global variables are like any other variables - only available in that page.

      Rather than define them as global, however, you should pass them as parameters
      to the functions where they are required.

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Barkster

        #4
        Re: Using variables in Two Function?

        This is the simplified way I have it now:

        $var = true;
        $var2 = 2;

        function getval() {
        display();
        }

        function display() {
        if($var) echo 'true' //not getting a value for $var?
        }

        <body>
        </php getval(); ?>
        </body>

        Comment

        • David Haynes

          #5
          Re: Using variables in Two Function?

          Barkster wrote:[color=blue]
          > This is the simplified way I have it now:
          >
          > $var = true;
          > $var2 = 2;
          >
          > function getval() {
          > display();
          > }
          >
          > function display() {
          > if($var) echo 'true' //not getting a value for $var?
          > }
          >
          > <body>
          > </php getval(); ?>
          > </body>
          >[/color]
          As others have said, there are many ways to do this. Either:

          function display($var) {
          if( $var ) echo 'true';
          ]

          would work, or:

          function display() {
          global $var;
          if( $var ) echo 'true';
          }

          would also work.

          -david-

          Comment

          • Barkster

            #6
            Re: Using variables in Two Function?

            Thanks, got it to work.

            Comment

            Working...