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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?