在 PHP 中,是否有一种将变量与多个值进行比较的简短方法? [英] In PHP, is there a short way to compare a variable to multiple values?

查看:17
本文介绍了在 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.

例如,类似这样的事情可能会有所帮助:

For example, something along the lines of this might help:

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

或任何可以减少打字的东西.

or anything that results in less typing.

推荐答案

in_array() 是我用的

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

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

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