php - PHP - 字符串连接中的三元运算符与 strtotime - “语法错误 - 意外')'”
问题描述
我有一个自定义函数,functions.php
它返回 HTMl 和一些数据。我需要通过条件传递其中的一些来检查它是否存在,否则不要打印它。
我所拥有的看起来像这样:
$webinarDate = get_field('webinar_date');
// more code between here
$filteredresourcesHtml .= '
<div class="cell small-12 medium-4 large-3 equal-height">
<a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
<i class="icon-' . $post_type . '"></i>
<p class="content-type">' . ucfirst($post_type_display) . '</p>
<h4>' . $shortTitle . '</h4>
<h5>' . ($webinarDate ? date("F d, Y", strtotime($webinarDate))) . '</h5>
</a>
</div>';
重要的一行是这样的:
<h5>' . ($webinar ? date("F d, Y", strtotime($webinarDate))) . '</h5>
上面的行给了我:
致命错误:未捕获的错误:语法错误,意外')'
PHP if... else 替代语法抛出“unexpected ':' in...”错误信息
所有这些都说要使用三元运算符,我正在这样做但是不清楚如何具体格式化它,并且上面的答案也没有提供关于如何利用strtotime
函数和输出日期的清晰说明。
这表明你需要一个 else 所以我尝试了:
<h5>' . ($webinarDate ? (date("F d, Y", strtotime($webinarDate)) : ('')) . '</h5>
但这给了我
致命错误:未捕获错误:语法错误,意外':'
如何在 strtotime 的字符串连接中使用三元运算符?这个的语法具体是什么?
我目前最接近的是:
<h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>
但在整个声明中:
$filteredresourcesHtml .= '
<div class="cell small-12 medium-4 large-3 equal-height">
<a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
<i class="icon-' . $post_type . '"></i>
<p class="content-type">' . ucfirst($post_type_display) . '</p>
<h4>' . $shortTitle . '</h4>
<h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>
</a>
</div>';
我在最后一个 div 上收到一个错误,上面写着
致命错误:未捕获错误:语法错误,意外';'
解决方案
您好,最适合您的是在变量中获取三元条件的值,然后将其连接到您想要的字符串中
$filteredresourcesHtml = condition ? $firstValue: $defaulValue
推荐阅读
- php - 如何在使用 maatwebsite/excel 包版本 3.1 插入之前检查数据库中是否存在记录
- apache-kafka - 如何将对象存储中的 csv 数据(只能使用 url 访问)加载到 kafka 主题中,并且是否有任何时间间隔来加载每一行?
- tensorflow - Tensorflow Object-detection API:如果帧中有多个相同的对象,我如何将输出转换为语音?
- php - 如何在wordpress中动态猫头鹰轮播多行?
- java - 如何并行运行布尔类型的 CompletableFutures 并在第一个 FALSE 结果时退出或在所有任务返回 TRUE 时返回?
- php - 在 Laravel 中搜索列但显示来自另一个表列的结果
- r - 闪亮 - 转到同一页面中的另一个部分
- spring-boot - org.springframework.transaction.TransactionSystemException:无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException
- php - 在 Codeigniter 中压缩图像
- excel - 从 VBA 或 VSTO 等访问新的 Excel 365 实时自定义数据类型