get outside variables to use in a user created function?

Discussion in 'PHP' started by dcgamers, Jul 18, 2007.

  1. #1
    I have a function that prints a variable that is on the page that the function is being called from but since functions don't do that, the variable appears as text. How can I make it so that when I echo out the variable, it finds the variable from the file where the function is being called from and prints it the way it should be.

    Thanks.
     
    dcgamers, Jul 18, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Functions have their own scope so they don't see regular variables outside their scope.

    You can pass the variable to the function or use a define:


    define("myVar", "hello world");

    function foo(){
    print constant("myVar");
    }

    foo();
     
    mrmonster, Jul 18, 2007 IP
  3. dcgamers

    dcgamers Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the problem is that I have many variables that need to be printed so is there any way to do this without defining which variables? Or perhaps only make a global variable out of variables in a specific string?
     
    dcgamers, Jul 18, 2007 IP
  4. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I personally would pass any variables I need to the function, but you could always do:

    
    $t = "hello world";
    
    function foo(){
    	global $t;
    	
    	print $t;
    }
    
    foo();
    
    PHP:
    A bit of a "black magic" method of doing things, not my style.
     
    mrmonster, Jul 18, 2007 IP
  5. dcgamers

    dcgamers Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You don't under stand. I have almost 50 variables, that all change from time to time. I can't make 50 some global variables. All of the variables are printed in a function through eval but since there not global they don't display. Can i make only the ones printed with eval global?
     
    dcgamers, Jul 18, 2007 IP
  6. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #6
    $GLOBALS['t'] might be helpful?

    50 variables, have you heard of arrays?
     
    mrmonster, Jul 18, 2007 IP
  7. dcgamers

    dcgamers Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Where might I put that?
     
    dcgamers, Jul 18, 2007 IP
  8. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I would suggest you paste your code, theres probably a better way to do what you are doing.
     
    mrmonster, Jul 18, 2007 IP