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

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>";
}