如何在PHP中组合两个IF语句 [英] How to combine two IF statements in PHP

查看:616
本文介绍了如何在PHP中组合两个IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

$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;


推荐答案

要么:

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);
    }
}

或者:

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".

这篇关于如何在PHP中组合两个IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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