PHP - 重构/改进Ifs
我有以下数据结构;PHP - 重构/改进Ifs
Array ([0] => Array ([suit] => c [rank] => A) [1] => Array ([suit] => d [rank] => A))
我想测试这些卡是否具有某些值并显示相应的消息;
if ( (($hand[0]['rank'] == "A") && ($hand[1]['rank'] == "A"))
|| (($hand[0]['rank'] == "K") && ($hand[1]['rank'] == "K"))
|| (($hand[0]['rank'] == "Q") && ($hand[1]['rank'] == "Q"))
|| (($hand[0]['rank'] == "A") && ($hand[1]['rank'] == "K"))
|| (($hand[0]['rank'] == "K") && ($hand[1]['rank'] == "A"))
) {
echo "Action: Raise pre-flop. Re-raise if already raised.<br />\n";
}
if (
(($hand[0]['rank'] == "7") && ($hand[1]['rank'] == "7"))
|| (($hand[0]['rank'] == "A") && ($hand[1]['rank'] == "J") && ($hand[0]['suit'] == $hand[1]['suit']))
|| (($hand[0]['rank'] == "J") && ($hand[1]['rank'] == "Q") && ($hand[0]['suit'] == $hand[1]['suit']))
|| (($hand[0]['rank'] == "10") && ($hand[1]['rank'] == "J") && ($hand[0]['suit'] == $hand[1]['suit']))
) {
echo "Action: Worth a call pre-flop (provided no-one has raised).<br />\n";
}
我已通过删除一些条件简化了IF。
任何人都可以提出一个更好的方法来做到这一点?
回答:
也许尝试使用in_array()方法
php.net in_array()
回答:
尝试使用这样的引用:
$rank0 =& $hand[0]['rank']; $rank1 =& $hand[1]['rank'];
if (
($rank0 == "A" && $rank1 == "A")
|| (...)
只是,作为第一步代码最小化
以上是 PHP - 重构/改进Ifs 的全部内容, 来源链接: utcz.com/qa/258747.html