在PHP中,有一个简单的方法来比较一个变量和多个值? [英] In PHP, is there a short way to compare a variable to multiple values?

查看:124
本文介绍了在PHP中,有一个简单的方法来比较一个变量和多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想知道是否有一种方法来缩短这样的:

Basically what I'm wondering if there is a way to shorten something like this:

if ($variable == "one" || $variable == "two" || $variable == "three")

以这样的方式,变量可以测试或与多个值进行比较,而不必每次重复变量和运算符。

in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.

例如,可能有帮助:

if ($variable == "one" or "two" or "three")

或任何可减少输入的内容。

or anything that results in less typing.

推荐答案

in_array() 是我使用的

in_array() is what I use

if (in_array($variable, array('one','two','three'))) {

这篇关于在PHP中,有一个简单的方法来比较一个变量和多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆