doujiangao4229 2014-11-18 23:30
浏览 53
已采纳

PHP - echo跳过文本[关闭]

So I have this simple code:

 foreach($items as $index=>$item)
 {
   echo "<strong>Item " . $index+1 . " (" . $item[0]->getNamedItem('name')->nodeValue .")</strong>";
 }

$items has only 1 item for now, which is an array of DOMDocument data from an XML file. $item[0]->getNamedItem('name')->nodeValue is "purchase";

However, the HTML printed is: 1 (purchase)

Why is this, and how can I correct it?

Thanks, ~Hom

  • 写回答

1条回答 默认 最新

  • doubingjian2006 2014-11-19 00:05
    关注

    Your $index may or may not be a number, so use the (int) in front to convert it. Next, put your mathematical operations in parentheses to avoid trying to "add" strings to the sum.

    foreach($items as $index=>$item)
    {
      echo "<strong>Item " . ((int)$index+1) . " (" . $item[0]->getNamedItem('name')->nodeValue .")</strong>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?