Php 如何在函数中返回函数
如果游戏中至少有一个玩家有21个,则Make函数有20个返回true,否则返回false。此函数应使用“二十一”函数Php 如何在函数中返回函数,php,Php,如果游戏中至少有一个玩家有21个,则Make函数有20个返回true,否则返回false。此函数应使用“二十一”函数 function has_twenty_ones($game){ function twenty_ones($game) { $players_with_score_21 = []; foreach ($game['players'] as $name => $player) { $distance =
function has_twenty_ones($game){
function twenty_ones($game)
{
$players_with_score_21 = [];
foreach ($game['players'] as $name => $player) {
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
if ($distance == 21) {
$players_with_score_21 = [$name];
}
}
return $players_with_score_21;
}
return isset($players_with_score_21);
}
函数有20个1($game){
功能二十一(游戏)
{
$players_,_分数为21=[];
foreach($name=>$player的游戏['players']as$name){
$distance=21-$player['score'];
如果($distance<0){
继续;
}
如果($distance==21){
$players_与_得分_21=[$name];
}
}
返回$players_和_分数_21;
}
返回isset($21分的玩家);
}
对其进行编码的最佳方法是什么?只需检查
twork\u ones
函数的返回值是否为空,如果返回值超过twork\u ones
值,则返回false
function has_twenty_ones($game){
function twenty_ones($game){
$players_with_score_21 = [];
foreach ($game['players'] as $name => $player) {
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
if ($distance == 21) {
$players_with_score_21 = [$name];
}
}
return $players_with_score_21;
}
$playersWithScore = twenty_ones($game);
if (!empty($playersWithScore)) {
return $playersWithScore;
} else {
return false;
}
}
函数有20个1($game){
功能二十一(游戏){
$players_,_分数为21=[];
foreach($name=>$player的游戏['players']as$name){
$distance=21-$player['score'];
如果($distance<0){
继续;
}
如果($distance==21){
$players_与_得分_21=[$name];
}
}
返回$players_和_分数_21;
}
$PlayersWithCore=二十元($game);
如果(!空($playerWithCore)){
返回$playersWithScore;
}否则{
返回false;
}
}
功能二十一($game)
{
$players_,_分数为21=[];
foreach($name=>$player的游戏['players']as$name){
$distance=21-$player['score'];
如果($distance<0){
继续;
}
如果($distance==21){
$players_与_得分_21=[$name];
}
}
返回$players_和_分数_21;
}
函数有20个1($game){
如果(计数($this->two_one($game))>0)
返回true;
其他的
返回false;
}
我不确定为什么需要两个函数来实现此功能
正如@riggsfully所提到的,您实际上并不是在调用throughones()函数。为什么没有以下代码:
function has_twenty_ones($game)
{
foreach($game['players'] as $name => $player)
{
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
// If at least one player has 21, return true.
if($distance == 21) {
return true;
}
}
return false;
}
函数有20个1($game)
{
foreach($name=>$player的游戏['players']as$name)
{
$distance=21-$player['score'];
如果($distance<0){
继续;
}
//如果至少有一个玩家有21个,则返回true。
如果($distance==21){
返回true;
}
}
返回false;
}
当遇到得分为21的玩家时,上述将返回true,否则将返回false。请显示您的
$game
数组。类似于print\r($game)首先,作为一个函数,您必须在外部函数的某个地方调用它!!!嗯!!!OP为什么要“使用此代码”?一个好的答案总会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,也是为了将来的访客,以便他们能够找到这个问题并阅读您的答案。@RiggsFolly我总是先发布解决方案,然后描述我所做的事情,但是当标题明确谈到函数中的函数时,您已经将函数移出函数,我们开始了,DV removedIt可能值得一提的是,在OP的代码中,他实际上并没有从外部函数调用内部函数,因此它不起作用也就不足为奇了。您的答案是错误的<代码>$players\u with_score\u 21
未在has\u twenth\u one
函数中定义。$this也未定义。顺便说一句,它总是返回布尔值。@kcp是的,我的意思是返回设置($this->twenty_one($game));我更新了上面的回复,但是当标题明确提到函数中的函数时,你已经将函数移出了函数。它仍然返回boolean
值,因为isset
函数只能返回boolean
我想你需要它,这样你就可以轻松地返回$this->tworu one($game);它返回你的数组
function has_twenty_ones($game)
{
foreach($game['players'] as $name => $player)
{
$distance = 21 - $player['score'];
if ($distance < 0) {
continue;
}
// If at least one player has 21, return true.
if($distance == 21) {
return true;
}
}
return false;
}