©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
[#1] Michael Newton (http://mike.eire.ca/) [2005-12-12 15:17:30]
The XML declaration does not need to be handled specially.
You should output it via an echo statement, in case your code is ever used on a server that is (poorly) configured to use short open tags.
But there's no need to treat the ?> at the end of the string specially. That's because it's in a string. The only thing PHP ever looks for in a string is \ or $ (the latter only in double-quoted strings.)
I have never had need for the following, as some have suggested below:
<?php
$xml=rawurldecode('%3C%3Fxml%20version%3D%221.0%22%3F%3E');
echo($xml);
?>
<?php echo '<?xml version="1.0" ?'.'>' ?>
<?php echo "<?xml version=\"1.0\"\x3F>" ?>
[#2] php [AT] jsomers [DOT] be [2005-09-23 00:37:37]
PEAR states:
Always use
<?php ?>
to delimit PHP code, not the
<?php ?>
shorthand. This is required for PEAR compliance and is also the most portable way to include PHP code on differing operating systems and setups.
It is these small things that enhance readability in group projects, or libraries.
[#3] p o r g e s at the gmail dot com server [2005-04-01 20:02:00]
mike at skew dot org, I believe the differentiation is that "x"-"m"-"l" as a PI target is explicitly excluded from the definition of processing instructions.
[#4] mike at skew dot org [2004-10-21 16:53:59]
mart3862 mentions "XML processing instructions" and quotes their syntax from the spec, but is mistaken in using
<?phpxml version="1.0" ...?>
as an example. This little bit of markup that appears at the beginning of an XML file is in fact not a processing instruction at all; it is an "XML declaration" -- or, if it appears in an entity other than the main document, a "text declaration". All three constructs are formatted slightly differently, although they all do begin and end with the same.
The difference between a processing instruction, an XML declaration, or a text declaration is more than just a matter of subtle differences in syntax, though. A processing instruction embodies exactly two opaque, author-defined pieces of information (a 'target' and an 'instruction') that are considered to be part of the document's logical structure and that are thus made available to an application by the XML parser. An XML or text declaration, on the other hand, contains one to three specific pieces of information (version, encoding, standalone status), each with a well-defined meaning. This info provides cues to the parser to help it know how to read the file; it is not considered part of the document's logical structure and is not made available to the application.
[#5] stooges_cubed at racerx dot net [2004-10-20 13:13:35]
In the note above about escaping XML/PHP style
<?xml tags, the following code was used:
<?php // Html safe containers
echo <<<EOD
<?xml version="1.0"?>
...all sorts of XML goes here...
Nothing will affect the output of this code until:
EOD;
?>
EOD is just an example stop/start name.
This works too:
<?php // Html safe containers
$myOutput = <<<MYHTMLSAFEOUTPUT
<?xml version="1.0"?>
<html>
<title>PHP Example</title>
<body>
<p>...all sorts goes here...</p>
</body>
</html>
MYHTMLSAFEOUTPUT;
echo $myOutput;
?>
Only disadvantage of using this is that all the code highlighting programs I've seen never get it right, making your code look eronous in the majority of viewers.
Another alternative is to keep the XML / HTML in a separate include file and read in when needed. I don't know how efficient/inefficient this is for (idiots like yourselves) small amounts of text.
xmlheader.txt:
<?phpxml version="1.0"?>
mypage.php:
<?php
include("xmlheader.txt");
?>
[#6] crtrue at coastal dot edu [2004-04-30 23:02:09]
Although you can use the above methods to pass a document off as a valid for the W3C parser, a simpler-and-perfectly-legal method of doing so is to simple declare the document type in a meta tag. Something along these lines (mind the values in 'content' - I haven't personally used the Content-Type method in awhile):
<meta http-equiv="Content-Type" content="application/xml+xhtml; charset=UTF-8" />
Of course if you're using just XML, and don't use such functions, then the above methods will work just as fine.
[#7] mart3862 at yahoo dot com dot au [2004-04-18 09:29:53]
Now the ultimate truth on how you should output xml processing instructions:
There have been several posts suggesting ways to include the text
<?phpxml version="1.0" encoding="utf-8"?>
in your output when short_tags is turned on, but only the following should be used:
<?php echo '<?xml version="1.0" ?'.'>' ?>
or
<?php echo "<?xml version=\"1.0\"\x3F>" ?>
Using one of these methods, and not making use of short tags, means your source code will also be a valid XML document, which allows you to do many things with it such as validation, XSLT translations, etc, as well as allowing your text editor to parse your code for syntax colouring. Every PHP tag will simply be interpreted as an XML processing instruction (commonly referred to as PI).
The reason why all the other suggested methods are not advisable is because they contain the characters ?> inside the PHP tag, which the XML parser will interpret as the end of the processing instruction.
A processing instruction is defined in XML as:
PI ::= '
<?php' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
to occur together within a processing instruction, unless they are delimiting the end of the tag. It also requires a PITarget (an identifier starting with a letter) immediately after the initial start delimiter, which means that all short tag formats are also invalid XML.
In other words, it explicitly forbids the characters ?>
Following these guidelines will result in code that is portable to servers with any configuration and allow you perform many useful tasks on your XML or XHTML source documents. Even if you do not intend to validate or translate your source documents, and you can ignore some incorrect syntax colouring in your text editor, it is still best to get into good habits early.
[#8] Anon [2004-02-21 18:05:25]
Yet another way of adding the XML processing instruction is to use:
<?php echo '<?xml version="1.0" ?'.'>' ?>
Because the ? and > are separated, the parser will not terminate before it is supposed to.
As a side note, the W3C's parser seems to recognise this method (assuming it even checks for the PI).
[#9] johnbeech at (not saying) mkv25 dot net [2003-12-07 16:42:34]
In the note above about escaping XML/PHP style
<?xml tags, the following code was used:
<?php // Html safe containers
echo <<<EOD
<?xml version="1.0"?>
...all sorts of XML goes here...
Nothing will affect the output of this code until:
EOD;
?>
EOD is just an example stop/start name.
This works too:
<?php // Html safe containers
$myOutput = <<<MYHTMLSAFEOUTPUT
<?xml version="1.0"?>
<html>
<title>PHP Example</title>
<body>
<p>...all sorts goes here...</p>
</body>
</html>
MYHTMLSAFEOUTPUT;
echo $myOutput;
?>
Only disadvantage of using this is that all the code highlighting programs I've seen never get it right, making your code look eronous in the majority of viewers.
Another alternative is to keep the XML / HTML in a separate include file and read in when needed. I don't know how efficient/inefficient this is for small amounts of text.
xmlheader.txt:
<?phpxml version="1.0"?>
mypage.php:
<?php
include("xmlheader.txt");
?>
[#10] dave at [nospam] dot netready dot biz [2002-03-18 04:21:05]
A little "feature" of PHP I've discovered is that the <?PHP token requires a space after it whereas after the <? and <% tokens a space is optional.
The error message you get if you miss the space is not too helpful so be warned!
(These examples only give a warning with error_reporting(E_ALL) )
<?PHP/*<Some HTML>*/?> fails...
<?/*<Some HTML>*/?> works...
[#11] mrtidy at mail dot com [2001-12-12 00:36:43]
[Ed Note:
This is because of short_tags,
<?phpxml turns php parsing on, because of the <?php.
interfering with the
--irc-html@php.net]
I am moving my site to XHTML and I ran into trouble with the <?phpxml ?>
<?php ?>
method of escaping for HTML. A quick check of the mailing list confirmed that the current preferred method to cleanly output the
<?phpxml ?>
line is to echo it:<br>
<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
\n"); ?>