如何为IF语句创建快捷方式? [英] How do i make shortcuts for IF statements?
问题描述
是否有一个速记版本,检查是否有许多变量具有相同的值?
is there a shorthand version of checking if numerous variables have the same value please ?
而不是: -
if ($a="valid")
{do stuff;}
if ($b="valid")
{do stuff;}
if ($c="valid")
{do stuff;}
if ($d="valid")
{do stuff;}
是这样的: -
if ($a or $b or $c or $d = "valid")
{do stuff;}
推荐答案
John Conde将数组放在一起然后使用 in_array
的解决方案是一个很好的解决方案。如果你想坚持基本的字符串比较,有点像你的示例代码,那么你可以这样做:
John Conde's solution of putting together an array and then using in_array
is a good solution. In case you want to stick with basic string comparisons, somewhat like in your example code, then you could do:
if ($a == 'valid' or
$b == 'valid' or
$c == 'valid' or
$d == 'valid') {
// DO STUFF
}
基于我从你的例子中注意到的事情,要记住一些事情代码:在PHP中进行相等性比较时,使用 ==
而不是 =
。单个 =
用于分配,而双 ==
用于比较。
Something to keep in mind, based on what I noticed from your example code: When doing comparisons for equality in PHP, use ==
and not =
. The single =
is for assignment, while the double ==
is for comparison.
这篇关于如何为IF语句创建快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!