在回显之前无法检索php变量

时间:2013-03-07 13:56:05

标签: php variables echo

我的代码中最近出现了一个奇怪的问题。我需要在if语句中检查变量值,但它总是失败,值为(false),在回显值之后一切正常。

示例(我记不清确切的代码):

for ($i=0; $i<=$limit; $i++) {
   if ($object->data[$i]['name']) {
      echo 'name: '.$object->data[$i]['name'];
   }
}

上面的脚本总是返回false(没有显示用户名)

下面的脚本工作了(即使我评论了echo语句)

for ($i=0; $i<=$limit; $i++) {
   // echo $object->data[$i]['name'];
   if ($object->data[$i]['name']) {
      echo 'name: '.$object->data[$i]['name'];
   }
}

这发生在任何人身上吗?有这个原因吗?

好的......这是原始代码:

<?php if ( $data->apartments[$i]['price']['price1']->value ) { ?>
<div class="prices">
   <table border="0" width="100%" cellpadding="0" style="border-collapse: collapse">
       <tr>
           <td colspan="2" style="border-bottom: 1px dashed #E36300">
               <h3>PRICE LIST</h3>
           </td>
       </tr>
       <tr><td>&nbsp;</td><td>&nbsp;</td></tr>

   <?php

   //$data->apartments[$i]['price']['price_date1']->value;
   //$data->apartments[$i]['price']['price_date2']->value;
   //$data->apartments[$i]['price']['price_date3']->value;
   //$data->apartments[$i]['price']['price_date4']->value;
   //$data->apartments[$i]['price']['price_date5']->value;
   //$data->apartments[$i]['price']['price_date6']->value;
   //$data->apartments[$i]['price']['price_date7']->value;
   //$data->apartments[$i]['price']['price_date8']->value;
   //$data->apartments[$i]['price']['price_date9']->value;

   //$data->apartments[$i]['price']['price1']->value;
   //$data->apartments[$i]['price']['price2']->value;
   //$data->apartments[$i]['price']['price3']->value;
   //$data->apartments[$i]['price']['price4']->value;
   //$data->apartments[$i]['price']['price5']->value;
   //$data->apartments[$i]['price']['price6']->value;
   //$data->apartments[$i]['price']['price7']->value;
   //$data->apartments[$i]['price']['price8']->value;
   //$data->apartments[$i]['price']['price9']->value;

   for ( $pc=0; $pc<=10; $pc++ ) {

       if ( $data->apartments[$i]['price']['price_date'.$pc]->value ) {
           echo "<tr>";
           echo "<td style='border: 1px solid #E36300' align='left' valign='top' width='50%'>".$data->apartments[$i]['price']['price_date'.$pc]->value."</td>";
           echo "<td style='border: 1px solid #E36300' align='right' valign='top' width='50%'>".$data->apartments[$i]['price']['price'.$pc]->value." ".$data->apartments[$i]['price']['price_currency']->value."</td>";
           echo "</tr>";
       } 
   }
   ?>

   </table>
</div>
<?php } ?>

1 个答案:

答案 0 :(得分:0)

问题可能是您尝试回显/输出并因此检查的变量是评估为 false 的变量。请注意,即使var填充/非填充,也有一些可以评估为false的事情。即 0 null 等..

要检查var是否已设置,请使用isset()。而不仅仅是($ var)..

但是,正如上面的评论所说,这只是猜测。您必须向我们/我提供确切的代码。