1

I'm not quite sure what I'm doing wrong, but it appears I'm having myself a brain fry trying to comprehend it..

$cards = array(range(1,52));
shuffle($cards);
echo $cards[0];

I get a array to string conversion error.

I've also tried a custom function to echo dependent on the input value and that isn't working either.

4 Answers 4

5

You're creating an array of arrays. range() already returns an array:

$cards = range(1,52);
shuffle($cards);
echo $cards[0];
Sign up to request clarification or add additional context in comments.

1 Comment

range(): "Create an array containing a range of elements."
1

The range function returns an array (http://php.net/manual/en/function.range.php), so the statement $cards = array(range(1,52)); has set $cards to be an array with exactly one element - an array containing the range of values from 1 to 52.

Thus when you try to echo $cards[0], you are trying to echo an element which is an array, which produces the error.

What you want to do is this:

$cards = range(1, 52);
shuffle($cards);
echo $cards[0];

1 Comment

Thank you for taking the time to explain it fully!
0

Get rid of the array, range returns an array:

$cards = range(1,52);

1 Comment

Thank you for fixing my issue !
0

Range already creates an array. You have create an array with one element that contains the array.

$cards =range(1,52);
shuffle($cards);
echo $cards[0];

1 Comment

Thank you for fixing my issue !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.