Jump to content

Help in finding whats wrong in my code


bprof

Recommended Posts

Hi,

I have a problem in this code I don't know why?


[code]function shufelmycards() {
        // we have to use some random stuff here.
        for ($i = 0; $i < 5; $i++) {
        $num = rand(1000,9999);
        mt_srand ((double) microtime() * 9999999);
        $allcards = mt_rand (0,35);
        do {
            mt_srand ((double) microtime() * 9999999);
            $allcards = mt_rand (0,35);
        } while (in_array($allcards,$mycards));
        array_push($mycards,$allcards);
        $oponentcards[$i] = $allcards;
        }
    }[/code]

Each time I call this function I get this error message:

in_array(): Wrong datatype for second argument in

array_push(): First argument should be an array in

I've already declared both arrays(mycards, allcards array) early in the script

[code]$mycards = array ();

$oponentcards = array ();[/code]
Link to comment
https://forums.phpfreaks.com/topic/11171-help-in-finding-whats-wrong-in-my-code/
Share on other sites

Did you create the mycards variable outside of the function? If you did you need to add the following bit of code after you define your function:
[code]function shufelmycards() {
    global $mycards; // add this line![/code]

Now your function will be able to access the $mycards variable.

Variables dont have global scope when being used inside a function. In order for your variables to be accessable from the function you need to declare them as global.
[!--quoteo(post=379964:date=Jun 4 2006, 12:38 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 4 2006, 12:38 PM) [snapback]379964[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Did you create the mycards variable outside of the function? If you did you need to add the following bit of code after you define your function:
[code]function shufelmycards() {
    global $mycards; // add this line![/code]

Now your function will be able to access the $mycards variable.

Variables dont have global scope when being used inside a function. In order for your variables to be accessable from the function you need to declare them as global.
[/quote]

Thanks alot for your help. I cant see what I want, but I can see something :) thanks alot.

Could you please help me find out why I can see what I want which is (Oppenents cards 4 covered, on uncover. Player cards 5 uncovered):

[code]$allcards = array(
"cards/6_hearts.gif" , "cards/7_hearts.gif" , "cards/8_hearts.gif" , "cards/9_hearts.gif" , "cards/10_hearts.gif" , "cards/11_hearts.gif" , "cards/12_hearts.gif" , "cards/13_hearts.gif" , "cards/14_hearts.gif" ,
"cards/6_spades.gif" , "cards/7_spades.gif" , "cards/8_spades.gif" , "cards/9_spades.gif" , "cards/10_spades.gif" , "cards/11_spades.gif" , "cards/12_spades.gif" , "cards/13_spades.gif" , "cards/14_spades.gif" ,
"cards/6_diamonds.gif" , "cards/7_diamonds.gif" , "cards/8_diamonds.gif" , "cards/9_diamonds.gif" , "cards/10_diamonds.gif" , "cards/10_diamonds.gif" , "cards/11_diamonds.gif" , "cards/12_diamonds.gif" , "cards/13_diamonds.gif" , "cards/14_diamonds.gif" ,
"cards/6_clubs.gif" , "cards/7_clubs.gif" , "cards/8_clubs.gif" , "cards/9_clubs.gif" , "cards/10_clubs.gif" , "cards/11_clubs.gif" , "cards/12_clubs.gif" , "cards/13_clubs.gif" , "cards/14_clubs.gif");

$values = array(
"6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" ,
"6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" ,
"6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" ,
"6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" );

function showcards($show,$title,$footer) {
        global $allcards;
        global $useraction;
        $showcards .= "<table width='70%' background='gfx/black.gif' cellspacing='   2' cellpadding='0' border='0'>
<tr><td valign='top'><TABLE width='100%' cellspacing='0' cellpadding='5' border='0'><TR><TD align='center' bgcolor='#000066'><img src=\"gfx/$title.gif\" alt='' border='0' align='top'></td></tr></table></td></tr>
<tr><td valign='top'><TABLE width='100%' cellspacing='0' cellpadding='5' border='0'><TR><TD align='center' bgcolor='#0066ff'>";
        for($i=0; $i < sizeof($show); $i++) {
        $card = $show[$i];
        if ($useraction != "deal" && $title == "dealershand" && $i == 0) {
            $showcards .= "<img src='cards/back.gif' width='71' height='96' alt='' border='0'>&nbsp;";
        } else {
            $showcards .= "<img src=" . $allcards[$card] . " width='71' height='96' alt='' border='0'>&nbsp;";
        }
    }
    $showcards .= "<br>$footer</td></tr></table></td></tr></table><br>";
    return $showcards;
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.