简体   繁体   中英

Echo a result from function php

I use the following functions in my PHP script:

function draw_dealer_card() {
//get a key
$key = array_rand($_SESSION["dealer_pile"]);
// add the card to the hand
$_SESSION["dealer_hand"][] = $_SESSION["dealer_pile"][$key];
echo $_SESSION["dealer_pile"][$key];
}

and this one:

function list_dealer_hand() {

}

draw_dealer_card() grabs a random card from the array and places it in the hand of the dealer. And the echo shows the cards the dealer has drawn.

And the list_dealer_hand() is empty for now.

And that is my problem, the list_dealer_hand() needs to contain the echo from draw_dealer_card() .

And i can't really find a way to switch them around.

I hope i provided you guys with enough information otherwise please let me know!

Thanks in advance!

You can pass it as a variable:

list_dealer_hand($value){
   echo $value;
}

Or store it in a session variable works too..

The function draw_dealer_card() could return the value instead of echo the value:

function draw_dealer_card() {
    //get a key
    $key = array_rand($_SESSION["dealer_pile"]);
    // add the card to the hand
    $_SESSION["dealer_hand"][] = $_SESSION["dealer_pile"][$key];
    return $_SESSION["dealer_pile"][$key];
}

function list_dealer_hand() {
   $return_value_of_draw_dealer_card = draw_dealer_card();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM