How do I return an variable and an array from a method in php?

145

php n00b here:

I'm trying to pick a card from a deck.

The class instance is '$test_1' and the function 'pick_card' is a method of that class.

I'd like the function to return 1) the card I want, and 2) the deck, less one card.

Where am I going wrong?

$card, $deck = $test_1->pick_card($play_deck);

public function pick_card(&$deck)
{
    $card = $deck[0];
    echo "picked this card: " . $card . "<br />";
    array_splice($deck,0,0);
    return $card; $deck;
}
967

Answer

Solution:

You can't return two variables like that in PHP. I note that you are passing$deck to the function by reference, so any modifications you make to that variable will be reflected in that variable as the function operates on it.

That being said, since this is a class, you really shouldn't have to pass a variable at all, you really should have a property that stores your deck. I would suggest something like this:

class card_deck {
    protected $deck = NULL;

    public function __construct() {
        // some logic to create, shuffle, and set deck array to $this->deck
    }

    public function pick_card() {
        if($this->get_card_count() < 1) {
            throw new Exception('The deck is empty.');
        }
        return array_shift($this->deck);
    }

    public function get_card_count() {
        return count($this->deck);
    }
}
15

Answer

Solution:

You will have to return it as an array. You can use an associative array or you can use list to break the array back out in to variables like this:

list($card, $deck) = $test_1->pick_card($play_deck);

public function pick_card(&$deck)
{
    $card = $deck[0];
    echo "picked this card: " . $card . "<br />";
    array_splice($deck,0,0);
    return array($card, $deck);
}
144

Answer

Solution:

since you've got an object you could also use$this

class card_deck {
  public $card;
  public $deck;
  public function pick_card(&$deck) {
    $this->card = $deck[0];
    $this->deck = $deck;
    echo "picked this card: " . $this->card . "<br />";
    array_splice($this->deck,0,0);
  }
}

$test_1 = new card_deck();
//set $play_deck 
$test_1->pick_card($play_deck);

// now use
$test_1->card;
$test_1->deck;
849

Answer

Solution:

You can return the values you need in an associative array. I think you are familiar with arrays, as you usedeck[0] in yourpick_card function:

class Foo{
    public function pick_card(&$deck){
        $card = $deck[0];
        echo "picked this card: " . $card . "<br />";
        array_splice($deck,0,0);
        return array('card'=>$card,'deck'=>$deck);
    }
}
$test_1 = new Foo;
$deck = $test_1->pick_card($play_deck);
echo 'Card: '.$deck['card'].'<br>';
echo 'Deck: '.$deck['deck'];

People are also looking for solutions to the problem: php - Confirm Form Resubmission while using JavaScript form submit

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.