嗨朋友一个简单的问题。
如何从 PHP echo 打印以下行
<?xml version="1.0" encoding="iso-8859-1"?>
我对转义序列感到困惑。
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
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
To take the XML output a step further, you may want to look into the simpleXML
extension in PHP.
See Here for more information
echo <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
XML;
您必须放置标题(让浏览器知道如何呈现显示):
header ("Content-Type:text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
使用单引号:
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
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