1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.
  2. Better Analytics for WordPress Get It Free

need help returning two values

Discussion in 'PHP' started by Felverick, Apr 2, 2007.

  1. #1
    Thanks for reading.

    Im currently working on an assignment where we are required to save two values to hidden variables. I need to be able to retrieve the values using a GetValues function as is required by the assignment. my question is how do i return these two values? returning one is fine but i am really unsure how to return two. here is my code so far for the SetValues function

    function SaveUserNameAndPassword($UserName,$Password)
    {
    echo "<input type=hidden name=\"$UserName\" value=\"$UserName\"><br/><br/>"
    echo "<input type=hidden name=\"$Password\" value=\"$Password\"><br/><br/>"

    }
     
    Felverick, Apr 2, 2007 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    first, you dont want to name them with $ signs.
    the value part is fine, cause PHP see's the $ sign and will echo that variables value.
     
    lbalance, Apr 2, 2007 IP
  3. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I need the whole script to better understand your situation.
     
    manilodisan, Apr 2, 2007 IP
  4. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is different concept but may be useful for you

    <?php
    $username = "";
    $pasword = "";

    function show($username,$pasword){
    global $username;
    global $pasword;

    $username = "dada";
    $pasword = "asda";
    }

    show();

    echo " This is the username ".$username;
    echo "<br>";
    echo " This is password ".$pasword;
    ?>
     
    jitesh, Apr 2, 2007 IP
  5. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If above is not confortable then view this
    ------------------------------------------
    function show(){
    $username = "adas";
    $password = "ssd";
    return $username."-".$password ;
    }

    Now
    ---------------
    $usr_pass = show();
    $data = explode("-",$usr_pass);
    $username = $data[0];
    $password = $data[1];
     
    jitesh, Apr 2, 2007 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When you say 'retrieve' the data, do you mean print them out within the function, or return them both from the function?

    If the former, your problem is that you're not using semi-colons at the end of your two lines. But you should have got an error with that and if so, I'm sure you would have told us that...

    If the latter, there's two ways you can do it: one is by passing the variables by reference so that when you change the variable within the function then the original value is changed, too. You can force passing a variable by reference by putting an ampersand before the variable in the function declaration. ie:
    function SaveUserNameAndPassword($UserName,$Password)
    becomes:
    function SaveUserNameAndPassword(&$UserName,&$Password)

    Alternatively, you could do something similar to jitesh's second recommendation. jitesh, no offence, but I don't like the way you did that... a) because you're setting an unnecessary restriction (by using a dash as a magic character, neither the username or password would be able to have one) and b) because you're returning a magic string, only to explode it into an array and access the variables when you could have just returned an array in the first place. So yeah, that's the second option: in your function, store the values in an array and return that array.
     
    TwistMyArm, Apr 3, 2007 IP