我有一个游戏,它将根据数字和花色显示10张随机卡片,但我需要检查一个数组,看看卡片是否已经显示。但是当我的本地数组$card通过该函数时,它并没有被保存。这是我所有的代码,现在,请尝试运行它,并告诉我我做错了什么,如果你想要他们提供的图像。
http://storealutes.com/blackjack/cards.zip
这是我的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;
}
?>发布于 2011-09-24 12:57:01
要访问函数内部的变量,请使用以下技术。
$GLOBALS['card'][] = array ($suit ." ". $number, hello);或
global $card;
$card[] = array ($suit ." ". $number, hello);发布于 2011-09-24 12:57:21
与大多数正常的语言不同,PHP中几乎没有词法作用域。因此,您的函数不能识别全局定义的变量。解决这个问题的简单方法是在函数中使用global $card;。
https://stackoverflow.com/questions/7537107
复制相似问题