Php 写If-else语句的更好方法
有没有更好的写作方法:Php 写If-else语句的更好方法,php,if-statement,coding-style,Php,If Statement,Coding Style,有没有更好的写作方法: if (in_array('WIN_WAITING', $statuses)) { $this->globalStatus = 'WIN_WAITING'; } else if (in_array('IN_PLAY', $statuses)) { $this->globalStatus = 'IN_PLAY'; } else if (in_array('WON', $statuses)) { $this->pay($this-&g
if (in_array('WIN_WAITING', $statuses)) {
$this->globalStatus = 'WIN_WAITING';
} else if (in_array('IN_PLAY', $statuses)) {
$this->globalStatus = 'IN_PLAY';
} else if (in_array('WON', $statuses)) {
$this->pay($this->tickets['ticketID']);
$this->globalStatus = 'WON';
} else if (in_array('PAYEDOUT', $statuses)) {
$this->globalStatus = 'PAYEDOUT';
} else if (in_array('CLOSED', $statuses)) {
$this->globalStatus = 'CLOSED';
} else if (in_array('LOST', $statuses)) {
$this->globalStatus = 'LOST';
} else if (in_array('OPEN', $statuses)) {
$this->globalStatus = 'OPEN';
}
这应该适合您: 在这里,我只是在所有的搜索字符串中循环,如果我找到了它,我就会打破循环
<?php
$search = ["WIN_WAITING", "IN_PLAY", "WON", "PAYEDOUT", "CLOSED", "LOST", "OPEN"];
foreach($search as $v) {
if(in_array($v, $statuses)) {
if($v == "WON") $this->pay($this->tickets['ticketID']);
$this->globalStatus = $v;
break;
}
}
?>
这应该适合您: 在这里,我只是在所有的搜索字符串中循环,如果我找到了它,我就会打破循环
<?php
$search = ["WIN_WAITING", "IN_PLAY", "WON", "PAYEDOUT", "CLOSED", "LOST", "OPEN"];
foreach($search as $v) {
if(in_array($v, $statuses)) {
if($v == "WON") $this->pay($this->tickets['ticketID']);
$this->globalStatus = $v;
break;
}
}
?>
也许像
$options = array('WIN_WAITING', 'IN_PLAY', 'WON', 'PAYEDOUT', 'CLOSED', 'LOST', 'OPEN');
for($i=0; $i<=7; $i++) {
if(in_array($options[$i], $statuses)) {
$this->globalStatus = $options[$i];
break;
}
}
没有测试,只是一个想法可能类似
$options = array('WIN_WAITING', 'IN_PLAY', 'WON', 'PAYEDOUT', 'CLOSED', 'LOST', 'OPEN');
for($i=0; $i<=7; $i++) {
if(in_array($options[$i], $statuses)) {
$this->globalStatus = $options[$i];
break;
}
}
未经测试,只是一个想法您可以通过将pin码存储在数组中并使用foreach LoopSide:Paydout应该是PAIDOUT来减少if else,这是英语拼写规则的另一个很好的例外!通过将pin码存储在数组中并使用foreach loopAside:paydout应该是PAIDOUT,您可以减少if-else,这是英语拼写规则的另一个很好的例外!你忘记了“赢”的条件@AkramFares感谢您的通知,更新了我的answer@AkramFares:我认为这可以留待OP考虑:-您忘记了“赢”的条件@AkramFares感谢您的通知,更新了我的answer@AkramFares:我想这可以留给OP去做:-