带有多个条件的 PHP If 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5593512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:55:22  来源:igfitidea点击:

PHP If Statement with Multiple Conditions

phpif-statement

提问by blasteralfred Ψ

I have a variable$var.

我有一个变量$var

I want echo "true"if $varis equal to any of the following values abc, def, hij, klm, or nop. Is there a way to do this with a single statement like &&??

我想要 echo "true"if$var等于以下任何值abcdefhijklm, 或nop。有没有办法用像&&??这样的单个语句来做到这一点。

回答by Pekka

An elegant way is building an array on the fly and using in_array():

一种优雅的方法是动态构建数组并使用in_array()

if (in_array($var, array("abc", "def", "ghi")))

The switchstatementis also an alternative:

switch语句也是一个替代方案:

switch ($var) {
case "abc":
case "def":
case "hij":
    echo "yes";
    break;
default:
    echo "no";
}

回答by Tokk

if($var == "abc" || $var == "def" || ...)
{
    echo "true";
}

Using "Or" instead of "And" would help here, i think

我认为使用“或”而不是“和”会有所帮助

回答by Shakti Singh

you can use in_array function of php

你可以使用 php 的 in_array 函数

$array=array('abc', 'def', 'hij', 'klm', 'nop');

if (in_array($val,$array))
{
  echo 'Value found';
}

回答by KingCrunch

Dont know, why you want to use &&. Theres an easier solution

不知道,为什么要使用&&. 有一个更简单的解决方案

echo in_array($var, array('abc', 'def', 'hij', 'klm', 'nop'))
      ? 'yes' 
      : 'no';

回答by sauerburger

you can use the boolean operator or: ||

您可以使用布尔运算符或: ||

if($var == 'abc' || $var == 'def' || $var == 'hij' || $var == 'klm' || $var == 'nop'){
    echo "true";
}

回答by Osh Mansor

You can try this:

你可以试试这个:

<?php
    echo (($var=='abc' || $var=='def' || $var=='hij' || $var=='klm' || $var=='nop') ? "true" : "false");
?>

回答by IC360 Oliver Cannell

I found this method worked for me:

我发现这种方法对我有用:

$thisproduct = "my_product_id";
$array=array("$product1", "$product2", "$product3", "$product4");
if (in_array($thisproduct,$array)) {
    echo "Product found";
}

回答by paul

Try this piece of code:

试试这段代码:

$first = $string[0]; 
if($first == 'A' || $first == 'E' || $first == 'I' || $first == 'O' || $first == 'U') {
   $v='starts with vowel';
} 
else {
   $v='does not start with vowel';
}

回答by Owais Akber

It will be good to use array and compare each value 1 by 1 in loop. Its give advantage to change the length of your tests array. Write a function taking 2 parameters, 1 is test array and other one is the value to be tested.

最好使用数组并在循环中 1 x 1 比较每个值。它可以改变测试数组的长度。编写一个带2个参数的函数,1个是测试数组,另一个是要测试的值。

$test_array = ('test1','test2', 'test3','test4');
for($i = 0; $i < count($test_array); $i++){
   if($test_value == $test_array[$i]){
       $ret_val = true;
       break;
   }
   else{
       $ret_val = false;
   }
}

回答by Orgmir

I don't know if $var is a string and you want to find only those expressions but here it goes either way.

我不知道 $var 是否是一个字符串,而您只想找到那些表达式,但无论哪种方式都可以。

Try to use preg_match http://php.net/manual/en/function.preg-match.php

尝试使用 preg_match http://php.net/manual/en/function.preg-match.php

if(preg_match('abc', $val) || preg_match('def', $val) || ...)
   echo "true"