PHP 函数中的本地数组?
我有一个游戏,将根据数字和花色显示十张随机牌,但我需要检查一个数组以查看该牌是否已显示。但是我的本地数组 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与大多数理智的语言不同,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.要访问函数内部的变量,请使用以下技术。
或者
To access a variable inside of a function use the follow techniques.
or