【问题标题】:How do I close this php statement correctly?如何正确关闭此 php 语句?
【发布时间】:2012-05-20 11:20:31
【问题描述】:

我试图从我从更复杂的行列表等中偷来的一些元素创建一个简单的列表,我只需要列出用逗号分隔的单行中的值。

<?php foreach ($document_items as $document_item)
                {
                    if ($document_item->document_id == $document->id)
                            {
                                    if (nbf_common::nb_strlen($document_item->product_code) > 0)
                                    {
                                    echo nbf_common::nb_strlen($document_item->product_code);
                                    } 
                            ;}  ?> 
                ;} ?>

                    <?php  } ?>

我得到的结果如下 "3 ;} ?> 3 ;} ?> 4 ;} ?> 3 ;} ?> "

提前致谢

弥迦

【问题讨论】:

    标签: php if-statement syntax


    【解决方案1】:

    试试这个

    <?php
    foreach ($document_items as $document_item)
                    {
                        if ($document_item->document_id == $document->id)
                                {
                                        if (nbf_common::nb_strlen($document_item->product_code) > 0)
                                        {
                                            echo nbf_common::nb_strlen($document_item->product_code);
                                        } 
                                } 
                    } 
    
    
    ?>
    

    更多详情PHP Tags

    【讨论】:

      【解决方案2】:

      更改#9、#10,删除#11 行。你正在做的是打印字符:;} ?&gt; 并且在语法上是错误的。这是正确的:

      <?php foreach ($document_items as $document_item)
      {
          if ($document_item->document_id == $document->id)
          {
              if (nbf_common::nb_strlen($document_item->product_code) > 0)
              {
                  echo nbf_common::nb_strlen($document_item->product_code);
              } 
          } // here
      } // here
      ?>
      

      同样对于“逗号分隔”部分,将所需值放入变量中并在末尾回显。 可能是这样的:

      <?php
      $string = '';
      foreach ($document_items as $document_item)
      {
          if ($document_item->document_id == $document->id)
          {
              if (nbf_common::nb_strlen($document_item->product_code) > 0)
              {
                  $string .= nbf_common::nb_strlen($document_item->product_code).',';
              } 
          }
      }
      
      echo rtrim($string, ','); // remove the last comma
      ?>
      

      或使用临时数组将它们粘贴到最后:

      <?php
      $lines = array();
      foreach ($document_items as $document_item)
      {
          if ($document_item->document_id == $document->id)
          {
              if (nbf_common::nb_strlen($document_item->product_code) > 0)
              {
                  $lines[] = nbf_common::nb_strlen($document_item->product_code);
              } 
          }
      }
      
      echo implode(',', $lines); // bind them with comma
      ?>
      

      【讨论】:

        【解决方案3】:

        我不知道你为什么有这么多额外的?&gt;。模式是:

        <?php
        
        // PHP code goes here
        
        ?>
        

        即每个&lt;?php 都有一个匹配的?&gt;;不多也不少。1


        1。除了@Mihai 在下面的评论中指出的情况......

        【讨论】:

        • 如果您不想将任何直接的 HTML 发送到浏览器,那么实际上较少是可以的。实际上,建议对类文件使用 且不带结尾 ?>,以确保不会意外向浏览器发送空格字符.如果您确实发送了空格或调用 header("Location: ".$url) 的内容稍后在该过程中将不起作用“警告标头已发送”。调试是地狱。
        • @MihaiStancu:是的,谢谢!我现在已经在我的回答中指出了这一点。但可以肯定的是,OP 的内容是错误的。
        【解决方案4】:

        要理解 php 标记,您可能会认为自己在 HTML 文件中,并且正在使用

        中断 HTML 流

        在编写仅 PHP 的文件(类、接口、函数特征列表和类似的无模板的应用程序代码分组)时,建议在文件的第一个字符处打开而不是结束。

        在编写模板文件时,建议使用 php 的 alternate syntax:

        <?php if($x): ?>
        <?php elseif($y): ?>
        <?php else: ?>
        <?php endif; ?>
        

        代替标准:

        <?php if($x) { ?>
        <?php } else if($y) { ?>
        <?php } else { ?>
        <?php } ?>
        

        【讨论】:

          猜你喜欢
          • 2015-12-31
          • 2013-10-09
          • 2021-06-12
          • 2020-11-07
          • 2013-06-20
          • 2022-08-03
          • 1970-01-01
          • 2012-07-01
          • 2023-03-16
          相关资源
          最近更新 更多