php - 有没有办法在变量中包含 if 语句?
问题描述
我试图在 php 变量中提出一个 IF 语句,然后返回该变量,但我收到一个错误:语法错误,意外的 'if' (T_IF)
$output .= '<tr>
<td>'.$row["case_no"].'</td>
<td>'.$row["description"].'</td>
<td>'.$row["claimant"]." VS ".$row["defendant"].'</td>
<td>'. if($row["court_date_assigned"] == 1){ echo "result" } .'</td>
<td>'.$row["court_date_assigned"].'</td>
<td>'.$row["court_date_assigned"].'</td>
<td>'.$row["court_date_assigned"].'</td>
</tr>
';
}
return $output;
解决方案
您可以使用三元运算符,并假设您在 $row['court_date_assigned'] 中有一个 bool,您可以使用严格匹配(因此类型也必须匹配)。更多信息:https ://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
您还混合了双引号和单引号(围绕“VS”)。
$output .= '
<tr>
<td>'.$row['case_no'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['claimant'].' VS '.$row['defendant'].'</td>
<td>'.($row['court_date_assigned'] === 1 ? $result : '') .'</td>
<td>'.$row['court_date_assigned'].'</td>
<td>'.$row['court_date_assigned'].'</td>
<td>'.$row['court_date_assigned'].'</td>
</tr>';
推荐阅读
- appium-android - Mobile Recorder 自动启动和关闭应用程序
- c++ - upper_bound 如何与自定义对象类型 val 一起工作?
- sass - 如何将 Sass 变量导出到 Javascript?
- vue.js - 添加 , 到数字输入框 vue
- xampp - XAMPP 无法打开 PHP 文件
- mysql - 如何在 MySQL 5.7 中使用变量实现按功能分区
- rust - 启用项目功能时启用 Rust nightly 功能
- c# - 如何在特定时间触发 .NET Core 3.1 托管服务?
- matlab - 多次单击鼠标
- serverless-framework - ServerlessError:未找到无服务器插件“sls”