php - 将变量与字符串进行比较不起作用
问题描述
所以我试图将一个变量与一个字符串进行比较,我什至回显了该变量以确保它与我正在比较的字符串相同,但它不起作用。
$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 语句。
解决方案
我给出了简单的变量$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;
}
推荐阅读
- javascript - 无法从 MongoDB 获取数据
- grails - 你能告诉我如何保存我的域类吗
- c# - msbuild Directory.Build.props 即使在非常基本的示例中也不起作用
- c++ - 对使用 Glib 事件循环的程序使用 Google 测试
- quarkus - 无法使用 GraalVM 生成本机图像
- javascript - 如何在 jquery Laravel 中使用翻译
- javascript - JSON 对对象中的相同值使用多个键
- locale - CraftCms2 中的 url 中没有语言代码,管理面板无法工作
- java - 如何打印全名的名字和首字母
- reactjs - react-table v7 排序行为:当第三次单击列标题时,排序被删除