首页 > 解决方案 > 将变量与字符串进行比较不起作用

问题描述

所以我试图将一个变量与一个字符串进行比较,我什至回显了该变量以确保它与我正在比较的字符串相同,但它不起作用。

    $output = str_replace(',', '<br />', $row['order_summary']);
    $firstBreak = strpos($output, '<br />');
    $firstWord = explode(' ',trim($output))[0];         
    if ($firstWord == 'first' || $firstWord == 'second') {
        $output = str_replace(',', '<br />', $row['order_summary']);
        $firstBreak = strpos($output, '<br />');
        if($firstBreak === false) {
            $output = "<b>$output</b>";
        } 
        else 
        {
            $output = '<b>' . substr($output, 0, $firstBreak) . '</b>' . substr($output, $firstBreak);
        }
        echo $output;
    }
    else
    {
        $output = str_replace('.', '<br /><br>', $row['order_summary']);
        $firstBreak = strpos($output, '<br />');
        $output = '<b>' . substr($output, 0, $firstBreak) . '</b>' . substr($output, $firstBreak);
        echo $output;
    }

$firstWord当我回显它时,first结果给了我,所以它显然应该通过 if 语句,而不是进入 else 语句。

标签: php

解决方案


我给出了简单的变量$output=first; 然后代码进入 if 循环。所以问题出在 $output变量上。

<?php

$output = 'first';
$firstBreak = strpos($output, '<br />');
$firstWord = explode(' ', trim($output))[0];
if ($firstWord == 'first' || $firstWord == 'second') {
    echo 'test';
    exit;
    $output = str_replace(',', '<br />', $row['order_summary']);
    $firstBreak = strpos($output, '<br />');
    if ($firstBreak === false) {
        $output = "<b>$output</b>";
    } else {
        $output = '<b>' . substr($output, 0, $firstBreak) . '</b>' . substr($output, $firstBreak);
    }
    echo $output;
} else {
    $output = str_replace('.', '<br /><br>', $row['order_summary']);
    $firstBreak = strpos($output, '<br />');
    $output = '<b>' . substr($output, 0, $firstBreak) . '</b>' . substr($output, $firstBreak);
    echo $output;
}

推荐阅读