如何在 PHP 中组合两个 IF 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5955678/
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 23:01:22  来源:igfitidea点击:

How to combine two IF statements in PHP

phpif-statement

提问by Ryan Cooper

Okay i know this is a newb question, but how would i go about only performing IF 2 if IF 1 (text: test appears in data string.) I've tried combining the two but end up with all sorts of issues. So if test doesnt show up the loops skipped, if it does then the regex code i have in IF 2 will be ran.

好的,我知道这是一个新问题,但是如果 IF 1(文本:测试出现在数据字符串中),我将如何仅执行 IF 2。我尝试将两者结合起来,但最终遇到了各种问题。因此,如果测试未显示跳过的循环,如果显示,则将运行我在 IF 2 中的正则表达式代码。

$data = 'hello world "this is a test" last test';


// IF 1 
if (stripos($data, 'test') !== false) {
}


// IF 2
if (preg_match('/"[^"]*"/i', $data, $regs)) {
$quote = str_word_count($regs[0], 1);
$data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

echo $data;

回答by Rich Bradshaw

Either:

任何一个:

if (stripos($data, 'test') !== false) {
    if (preg_match('/"[^"]*"/i', $data, $regs)) {
        $quote = str_word_count($regs[0], 1);
        $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
    }
}

Or:

或者:

if (stripos($data, 'test') !== false && preg_match('/"[^"]*"/i', $data, $regs)) {
    $quote = str_word_count($regs[0], 1);
    $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

Both do the same thing. The &&operator means "and". The ||operator means "or".

两者都做同样的事情。该&&操作装置“和”。该||操作装置“或”。

回答by FrustratedWithFormsDesigner

Do you mean you want to nest one inside the other?

你的意思是你想把一个嵌套在另一个里面?

if (stripos($data, 'test') !== false)
{

  if (preg_match('/"[^"]*"/i', $data, $regs))
  {
     $quote = str_word_count($regs[0], 1);
     $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
  }

}

You could also change this to use &&(which means "And"):

您也可以将其更改为使用&&(这意味着“和”):

if (stripos($data, 'test') !== false && preg_match('/"[^"]*"/i', $data, $regs)) {
            $quote = str_word_count($regs[0], 1);
            $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

Also, your code uses !==. Is that what you meant or did you mean !=? I believe they have different meanings - I know that !=means "Not equal" but I'm not sure about !==.

此外,您的代码使用!==. 这是你的意思还是你的意思!=?我相信它们有不同的含义——我知道这!=意味着“不相等”,但我不确定!==.

回答by ghoppe

Simply nest your IF statements

只需嵌套您的 IF 语句

if (stripos($data, 'test') !== false) {
    if (preg_match('/"[^"]*"/i', $data, $regs)) {
        $quote = str_word_count($regs[0], 1);
        $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
    }

}

Or have I misunderstood your question?

还是我误解了你的问题?

Saying "I've tried combining the two but end up with all sorts of issues" is quite vague. Combining how? Nested like this? What issues?

说“我已经尝试将两者结合起来,但最终遇到了各种各样的问题”是相当含糊的。怎么结合?这样嵌套?什么问题?

回答by Fredefl

if (stripos($data, 'test') !== false) {
  if (preg_match('/"[^"]*"/i', $data, $regs)) {
  $quote = str_word_count($regs[0], 1);
  $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
  }
}