简体   繁体   English

PHP中的三元运算符

[英]Ternary Operator in PHP

Why this code do not want to work ? 为什么这段代码不想工作?

<?php
$currMonth='01';
?>

<a class="btn btn-xs btn-<?php ($currMonth=='01') ? 'primary' : 'default'; ?>-outline">

My output is empty: 我的输出为空:

<a class="btn btn-xs btn--outline">

Thanks. 谢谢。

It is working. 这是工作。 It is just not outputting something because you haven't told php to do so. 它只是不输出任何东西,因为您没有告诉php这样做。 Change <?php to eg <?php echo . <?php更改为例如<?php echo

please use following code... 请使用以下代码...

"echo " is missing 缺少“回声”

<?php
$currMonth='01';
?>

<a class="btn btn-xs btn-<?php echo ($currMonth=='01') ? 'primary' : 'default'; ?>-outline">

You can use <?= this operator 您可以使用<?=此运算符

<a class="btn btn-xs btn-<?= ($currMonth=='01') ? 'primary' : 'default'; ?>-outline">

or use echo as other suggested 或使用echo作为其他建议

You can read more about it on site below 您可以在下面的网站上了解更多信息

http://php.net/manual/en/function.echo.php

echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. echo还具有一种快捷语法,您可以在其中立即在开始标记后加上等号。 Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled. 在PHP 5.4.0之前,此简短语法仅在启用short_open_tag配置设置时有效。

没有输出字符串的功能!

<?php echo ($currMonth=='01') ? 'primary' : 'default'; ?>

Try this and your code will work: 试试看,您的代码将起作用:

<?php
$currMonth='01';
?>

<a class="btn btn-xs btn-<?php echo ($currMonth=='01') ? 'primary' : 'default'; ?>-outline">

This is a simple solution 这是一个简单的解决方案

<?php
$currMonth='01';
?>

<a class="btn btn-xs btn-<?php echo ($currMonth==='01') ? 'primary' : 'default'; ?>-outline">Test</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM