Home > Backend Development > PHP Tutorial > Instructions for using the simplexml_load_string function in PHP_PHP tutorial

Instructions for using the simplexml_load_string function in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:32:44
Original
823 people have browsed it

First use a piece of code to reproduce the problem
At first glance, the result is very confusing:

Copy the code The code is as follows:

$string = <<
hello
world

EOF;

$data = simplexml_load_string($string);

print_r($data);
print_r($data->foo);
?>

At first glance, the result is puzzling:
Copy code The code is as follows:

SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] = > world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)

Obviously print_r displays foo is an array with two bar elements, but in the end only one bar element is displayed!
The reason is actually very simple. In the result of simplexml_load_string shown above, foo is not an array, but an iterable object!
You can confirm like this:
Copy code The code is as follows:

foreach ($data->foo as $v ) print_r($v);
foreach ($data->children() as $v) print_r($v);

It seems that representations such as print_r or var_dump are not It’s not entirely trustworthy, so be more careful.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322732.htmlTechArticleFirst use a piece of code to reproduce the problem. At first glance, the result is very confusing: Copy the code and the code is as follows: ? php $string = EOF data foobarhello/bar/foo foobarworld/bar/foo /data EOF; $data...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template