Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a game that will display ten random cards based on number and suit, but I need to check an array to see if the card has already been displayed. But my local array $card is not being saved when it passes through the function. Here is all my code for right now please try running it and tell me what I am doing wrong if you want the images they are avaiable at.

http://storealutes.com/blackjack/cards.zip

here is my php:

    <?php
                                     //suit 1=Clubs | 2=Hearts | 3=Spades | 4=Diamonds//
                                     //Color 1=1or11 | 2-10=#   | 11-12=10//
$number;
$suit;
$card = array();

function newcard($number,$suit,$card){

$arrsuit = array (clubs, hearts, spades, diamonds);
$arrnumber = array (a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k);

$number = $arrnumber[rand(0,12)];                //Creates card value
$suit   = $arrsuit[rand(0,3)];       //Create card suit
$card   .= array  ($suit ." ". $number, hello);       //difines card name
return "<img src='cards/" . $suit . "-" . $number . "-150.png'/>";
}
for($i = 0; $i < 10; $i++){
echo newcard($number,$suit,$card);
}
echo $number;
foreach($card as $value){
    echo $value;
}


?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

To access a variable inside of a function use the follow techniques.

$GLOBALS['card'][] = array  ($suit ." ". $number, hello);

or

global $card;
$card[] = array  ($suit ." ". $number, hello);
share|improve this answer
    
I tried this I think I need to insert new values into the array or something, im not sure when i use the above code it echoes "Array" instead of the suit and card number. –  Sterling Lutes Sep 24 '11 at 16:21
    
Now im thinking ny echo needs to be different the one out of the function do i need global $card; the link before the foreach? –  Sterling Lutes Sep 24 '11 at 16:28

Unlike most sane languages, there is little sense of lexical scope in PHP. So, your function doesn't recognize variables defined globally. The easy fix for this is to use global $card; inside of your function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.