PHP 函数中的本地数组?

发布于 2024-12-06 09:43:49 字数 1078 浏览 2 评论 0原文

我有一个游戏,将根据数字和花色显示十张随机牌,但我需要检查一个数组以查看该牌是否已显示。但是我的本地数组 $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;
}


?>

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;
}


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-12-13 09:43:49

与大多数理智的语言不同,PHP 中几乎没有词法范围的意义。因此,您的函数无法识别全局定义的变量。解决这个问题的简单方法是在函数内部使用 global $card;

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.

梦回梦里 2024-12-13 09:43:49

要访问函数内部的变量,请使用以下技术。

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

或者

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文