PHP SimpleXML用法的语法说明

时间:2012-10-02 17:08:44

标签: php syntax

我参考了PHP手册,但由于早期关闭php标签,我得到了语法错误 - 显然我是一个新的PHP,但我只是试图将xml指定为php字符串。问题是什么?

<?php
$xmlstr = <<<XML //Need a clarification on '<<<XML' if possible 
<?xml version='1.0' standalone='yes'?> //This is closing my php script and causing error
<details>
<detail>
    <user>mcgraw</user>
    <dateA>09/11/1973</dateA>
    <inTime>9:00am</inTime>
    <outTime>6:00pm</outTime>
    <hours>9</hours>
    <notes>Monday</notes>
</detail>
<detail>
    <user>simpson</user>
    <dateA>08/23/1983</dateA>
    <inTime>9:00am</inTime>
    <outTime>5:30pm</outTime>
    <hours>8.5</hours>
    <notes>Thursday</notes>
</detail>
</details>
XML;
?>

1 个答案:

答案 0 :(得分:3)

这就是所谓的PHP的Heredoc语法。它可以帮助您标记字符串,而无需担心引号。

一个更简单的例子是:

$string = <<<STR_END_DELIMITER
This is some string, I can use ' and " freely, and it wouldn't break the string.
I can also use $variables inside, and they'll evaluate.
When I'm done with the string, I'll need to type STR_END_DELIMITER; on a fresh line, without anything else (not even spaces!)
STR_END_DELIMITER;
do_stuff_with_string($string);

有关详细信息,请参阅 PHP Manual Entry on Strings