#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76

    Vaiables between two functions


    Is there a way to use a variable like a global variable between two functions?
    PHP Code:
    function get_folders () {
    if (
    $handle opendir('products')) {
    $blacklist = array('.','..','products''index.php');
    while (
    false !== ($file readdir($handle))) {
             if (!
    in_array($file$blacklist)) {
    echo 
    '<a href=/products_list.php?folder=' $file '&amp;''pdf=' '/>'strtoupper($file) . '</a>''<br/>';

             }
    }
    closedir($handle);
    }
    }
    function 
    get_products() {
    //if ($handle = opendir('products/category1/product1')) { <<Remove this
    if ($handle opendir($file)) {  <<try this
    $blacklist 
    = array('.','..','index.php');
    while (
    false !== ($product readdir($handle))) {
             if (!
    in_array($product$blacklist)) {
    echo 
    '<a href=/products_list2.php?folder=' $product '&amp;''pdf=' '/>'strtoupper($product) . '</a>''<br/>';

             }
    }
    closedir($handle);
    }

    I was trying something like this but get a message of bool(false) outside of the function I have :
    global $file;
    PHP Code:
      if ($handleopendir($GLOBALS['file'])) 
    Last edited by mallen; December 21st, 2012 at 02:03 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    You could define a class to manage the IO then you don't have to worry about globals.

    P.S. Please use [ PHP ] tags for your code (see the sticky at the top of this forum).
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    the correct way of passing values to a function is by using the parameters:
    PHP Code:
    function square($x) {
        return 
    $x $x;
    }

    echo 
    square(3); 
    I know that PHP people looove global variables. But pulling values from some global scope should be an exception, not the rule.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    What does IO mean?
    Originally Posted by gw1500se
    You could define a class to manage the IO then you don't have to worry about globals.

    P.S. Please use [ PHP ] tags for your code (see the sticky at the top of this forum).
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    I/O is inputs/outputs.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo