Php 在Echo语句中使用三元运算符

Php 在Echo语句中使用三元运算符,php,ternary-operator,Php,Ternary Operator,我试图使用三元运算符,但结果不正确。我按照手册做了,但不知道发生了什么。控制台中的输出很混乱 php echo '<div class="item'.(($month_number==='09' && $month_year==='2017')?' active"':"").'>'; echo'; 控制台输出 <div class="item><div class=" m_page="" outer-div"=""> <div cla

我试图使用三元运算符,但结果不正确。我按照手册做了,但不知道发生了什么。控制台中的输出很混乱

php

echo '<div class="item'.(($month_number==='09' && $month_year==='2017')?' active"':"").'>';
echo';
控制台输出

<div class="item><div class=" m_page="" outer-div"="">
<div class="inner-div" style=""><div class="momo_p"> 
echo'检查您的报价

echo '<div class="item'.(($month_number==='10' && $month_year==='2017') ? "active" : "").'">
echo'

在三元运算符之后关闭class属性的双引号:

echo '<div class="item' . (($month_number==='09' && $month_year==='2017') ? ' active' : '') . '">';
echo';
使用以下代码:

PHP

$active = ($month_number==='09' && $month_year==='2017') ? 'active': '';
echo '<div class="item '.$active.'">';
$active=($month\u number=='09'和&$month\u year=='2017')?'活动':'';
回声';

这里是对一行程序的修改,添加了两个变量,以测试代码,从而使真正的结果将“active”连接到“item”。假结果导致“item”与空字符串连接,如下所示:

<?php
$month_number = "08";
$month_year   = "2016";

echo "<div class=\"item";

// making the ternary expression more manageable
$month_result = ($month_number === "09");
$year_result  = ($month_year === "2017");
echo  ($month_result && $year_result)? "active":"";

echo "\"></div>";
$str =  ($month_result && $year_result)? "active":"";
echo "<div class=\"item" . $str  . "\"></div>";

Try
echo'
使用
var\u dump($month\u number)
var\u dump($month\u year)
检查变量的值和类型,并共享结果感谢尝试,但由于某些原因,现在我只得到一个空白页。@BallobalFight您可以检查页面源代码是否存在?是的,除了Hm之外的所有内容都存在。不幸的是,与ravisachaniya相同。现在是空白页。如果您的输出是:使用上面的代码,它将帮助您解决问题@气球大战
$str =  ($month_result && $year_result)? "active":"";
echo "<div class=\"item" . $str  . "\"></div>";