-2

嗨朋友一个简单的问题。

如何从 PHP echo 打印以下行

<?xml version="1.0" encoding="iso-8859-1"?>

我对转义序列感到困惑。

4

6 回答 6

1

Printing the XML

You can either escape the double quotes like so:

echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";

or you can use single quotes, and therefore no escaping is needed:

echo '<?xml version="1.0" encoding="iso-8859-1"?>';

Alternatively, you can also use the print() method like so:

print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";

or

print '<?xml version="1.0" encoding="iso-8859-1"?>';

Which one you use, depends on what you would like to achieve. In most cases echo is the better option as it is ever so slightly faster.

See Here for the differences between echo and print

Outputting XML to the browser

However, if you are trying to generate XML, then it is also important to output the correct headers for the document, like so:

header("Content-Type: text/xml");

This will tell the browser to interpret what you output, as XML

Advanced XML Outputting

To take the XML output a step further, you may want to look into the simpleXML extension in PHP.

See Here for more information

于 2013-02-05T06:50:06.500 回答
0
echo <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
XML;
于 2013-02-05T06:53:13.483 回答
0

您必须放置标题(让浏览器知道如何呈现显示):

header ("Content-Type:text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
于 2013-02-05T06:53:23.343 回答
0

使用单引号:

echo '<?xml version="1.0" encoding="iso-8859-1"?>';
于 2013-02-05T06:43:38.377 回答
0
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
于 2013-02-05T06:43:43.050 回答
0

you can screen them using "\" (if you have some var in this statement) i.e. echo "<?xml version=\"1.0\" encoding=\"iso-8859-1"?>\";
If no var then just '<?xml version="1.0" encoding="iso-8859-1"?>'
Or you can use $text = '<?xml version="1.0" encoding="iso-8859-1"?>'; echo $text;
Or you can use Heredoc (use it for big text only)

And read about difference between ' and " in PHP

于 2013-02-05T06:49:40.557 回答