【问题标题】:PHP If Statement with Multiple Conditions in one [duplicate]PHP If语句在一个中具有多个条件[重复]
【发布时间】:2017-09-28 08:15:20
【问题描述】:

如何检查 PHP 多个条件语句。 像这样的东西:

if($image_ex == "{gif|jpg|png|jpeg}"){
     //something
}

【问题讨论】:

  • 使用逻辑运算符
  • 使用 ||逻辑运算符。
  • 另外,注意打字错误? "{gif|jpg|png|jpeg}")
  • 我知道,但我需要这样的东西。
  • 从技术上讲,您的问题的答案是您可以使用逻辑运算符在 if 语句中放置许多条件。尽管更好的方法是使用带有preg_match 的正则表达式或@chris85 的答案中的数组。

标签: php


【解决方案1】:

您需要使用正则表达式、使用多个条件或将值放入数组中。我会使用http://php.net/manual/en/function.in-array.php

if (in_array($image_ex , array('gif', 'jpg', 'png', 'jpeg'))) {

正则表达式方法可能是:

if(preg_match('/^(?:gif|png|jpe?g)$/', $image_ex)){

或长多条件方法:

if($image_ex == "gif" || $image_ex == "jpg" || etc..){

【讨论】:

  • 没想到in_array()
猜你喜欢
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多