0

当我开始使用 php、xml 和所有这些有趣的东西时。

在w3school 这个示例的 php代码中,我添加了一个变量:

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');

//添加变量

    $w=$x->item($i)->getElementsByTagName('w');

.

    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" . 
          $z->item(0)->childNodes->item(0)->nodeValue . 
          "' target='_blank'>" . 
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>

//在循环中添加了一些文本的var:

         <p>textLabel: " . $w->item(0)->childNodes->item(0)->nodeValue . " </p>";

我的 xml 包含这样的内容:

<title>1</title>
<url>5</url>
<w></w>

我想检查变量'w'是否为空,在我的情况下,它足以测试一个整数,也许像这样?:

if (is_int($w)) {
    # code...
  }

我尝试使用 if 语句修改 php 代码,但每当我尝试插入它时,我只会产生意外的 if_t 错误。

所需的最终结果是显示 labelText 和如果 int 中有一个值,则不显示任何内容。

编辑

通过var_dump()我的变量,我正在弄清楚发生了什么。空节点返回 NULL,所以很高兴知道。

我继续插入 if 语句:

if ($c->item(0)->nodeType==1) {
  //find a name matching the search text
  if (stristr($c->item(0)->childNodes->item(0)->nodeValue,$q)) {
    if ($hint=="") {
      //new if statement to check if node value is NULL
      if (is_null($f->item(0)->childNodes->item(0)->nodeValue)) {
          $lbl="";
      } else{$lbl='some text to display or not';
      }

      $hint=

然后我将 $lbl 连接到我想要文本的地方,它起作用了!

我确信有更好的方法,但是在我的编程经验中,我很高兴能弄清楚!Appriciate提示以更好的方式做到这一点:)

4

0 回答 0