Multiple variables inside functions

Discussion in 'PHP' started by audax, Jan 24, 2007.

  1. #1
    I'm trying to create a function that will display a piece of code that contains a variable outside of the function (IE Global variable)

    For instance, ideally this is what I'm trying to do:
    
    //Multiple different variables
    $first = "John";
    $last = "Doe";
    $age = "36";
    $eyes = "Blue";
    
    //Function to Display formatted variable
    function displayVariable($spec, $var){
         if (isset($var)){     
              echo $spec.': '.$var.'<br />;
         }
    }
    
    displayVariable("First Name",$first);
    displayVariable("Last Name",$last);
    
    PHP:
    and get it to output:

    First Name: John
    Last Name: Doe
    Code (markup):
    without having to do tons of global variables. Am I on the right track?
     
    audax, Jan 24, 2007 IP
  2. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    yeh absolutely correct mate
     
    rays, Jan 24, 2007 IP
  3. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #3
    picouli, Jan 25, 2007 IP