使用php从$_GET读取的数字没有被读入simplexml xpath查询


number from $_GET not being read into simplexml xpath query using php

THIS WORKS:

1 $number  = 2;
2 $allofit = simplexml_load_file("thexmlfile.xml");
3 $thebook = $allofit -> booklist[$number] -> abook;
4 echo $thebook;

下面的代码不能工作:但是如果我想用GET方法从一个from中读取$number,我需要设置

 6 $number=$_GET[thenumber];   // $number=2 from the form//
 7 echo $number;              // and properly shows $number=2 --*/
 8 $allofit = simplexml_load_file("thexmlfile.xml");
 9 $thebook = $allofit -> booklist[$number] -> abook;
 10 echo $thebook;

第10行的echo没有报告任何内容(没有错误,只是html中的空白),尽管我可以成功地在第7行echo $number…所以它被设置了,但是在第9行没有被拾取。

大家有什么想法吗?

提前感谢J

美元数量= $ _GET[数字];

在这个阶段$number是一个字符串。

需要将其强制转换为整数吗?可能有间隔?http://php.net/manual/en/function.intval.php

尝试:

$number=intval($_GET[thenumber]); 

PHP应该可以毫无问题地将你的"2"转换为2。我猜你漏掉了引号:$number=$_GET[thenumber]。试试$number=$_GET['thenumber'];