Professional Documents
Culture Documents
<?php
if($true)
{
echo "true";
}
else
{
echo "false";
}
?>
<?php
if($true)
echo "true";
else
echo "false";
?>
<?php
if($true):
echo "true";
else
echo "false";
endif;
?>
Some have said that using the 'echo' command is cleaner and more efficient.
However, as the manual points out, the method of breaking out of PHP is more
efficient when dealing with large sections of HTML. Because there is less
work for the parser.
Geekman at Textbook Torrents dot com 05-Mar-2007 01:19
Regarding the comment by rosswilliams at advocacytechnologies dot org:
Your suspicion is correct. The following all behave exactly the same:
<?php
// use echo to do the same thing - more effecient and easier to read in my
opinion
if ($true_or_false) {
echo '<p>The value of $true_or_false is true.</p>';
} else {
echo '<p>The value of $true_or_false is false.</p>';
}
?>
alfridus 23-Jul-2006 11:53
Only this work:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
echo $xml;
?>
with space after '<?php' and before ' ?>', no spacing between
'<?xml' and a semicolon after '"no"?>';'.
brettz9 at yahoo dot com 02-Apr-2006 03:04
I've essentially tried to synthesize this
discussion at
http://en.wikibooks.org/wiki/Programming:
Complete_PHP/Escaping_from_HTML
<?php
$version = "1.0";
print <<<HERE
<?xml version="
HERE;
//"
print $version."\"?>";
?>
Christoph 17-Jan-2006 01:08
Here's an inspiration on how to quickly fix all scripts relying on
short_open_tag being enabled:
find -name '*.php' | xargs perl -pi -e 's/<\?= ?(.*?) ?\?>/<?php echo($1); ?
>/g'
find -name '*.php' | xargs perl -pi -e 's/<\?/<?php/g'
find -name '*.php' | xargs perl -pi -e 's/<\?phpphp/<?php/g'
Michael Newton (http://mike.eire.ca/) 12-Dec-2005 11:17
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);
?>
Always use <?php ?> to delimit PHP code, not the <? ?> 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 are these small things that enhance readability in group projects, or
libraries.
pablo [] littleQ.net 24-Jul-2005 09:06
Just another more "feature" of IE...
Regards
01karlo at gmail dot com 26-Jun-2005 03:44
Or, use the following:
<?php
$xml=rawurldecode('%3C%3Fxml%20version%3D%221.0%22%3F%3E');
echo($xml);
?>
What is does it the value of the variable $xml is the RAW Url Encoded version
of the XML thing.
Then it decodes it and echo it to the visitor.
p o r g e s at the gmail dot com server 02-Apr-2005 04:02
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.
Lachlan Hunt 29-Mar-2005 05:06
The person that suggested the use of this meta element above is wrong:
That meta element and the XML declaration serve completely different purposes,
and that meta element should not be used. Such information should be set
using the HTTP Content-Type header (see the header() function).
Any XHTML page that just uses that meta element without proper HTTP Content-
Type header, will be processed as text/html by browsers regardless, and when
the HTTP headers do serve as application/xhtml+xml (or other XML MIME type),
that charset parameter in the meta element will be ignored.
mike at skew dot org 21-Oct-2004 11:53
mart3862 mentions "XML processing instructions" and quotes their syntax from
the spec, but is mistaken in using
echo <<<EOD
<?xml version="1.0"?>
...all sorts of XML goes here...
Nothing will affect the output of this code until:
EOD;
?>
$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:
<?xml version="1.0"?>
mypage.php:
<?php
include("xmlheader.txt");
?>
crtrue at coastal dot edu 01-May-2004 06:02
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):
Of course if you're using just XML, and don't use such functions, then the
above methods will work just as fine.
mart3862 at yahoo dot com dot au 18-Apr-2004 04:29
Now the ultimate truth on how you should output xml processing instructions:
There have been several posts suggesting ways to include the text <?xml
version="1.0" encoding="utf-8"?> in your output when short_tags is turned on,
but only the following should be used:
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.
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).
TarquinWJ 06-Feb-2004 02:54
Not spotted any messages like this one - delete it if there was one.
My hosting service allows <? and ?>, but I like to use valid XHTML, so I came
up with this simple solution:
It is possible to use the short tags <? ?> with XHTML or XML documents. The
only problem is that X(HT)ML requires a declaration using <? and ?>
This inserts a blank piece of PHP in between the < and ?, and when parsed will
output the regular tag
<?xml version="1.0" encoding="UTF-8"?>
mwild at iee dot NO_SP_AM dot org 19-Dec-2003 01:12
The text between <script> and </script> in XHTML is PCDATA, so < and &
characters in it should be interpreted as markup. This is a bit limiting for
PHP, which is often used to output tags, though you can of course use < and
& instead. To avoid that, which makes your code look peculiar and is easy
to forget to do, you can mark the PHP as CDATA, eg :
<script language="PHP">
//<![CDATA[
echo('Today is <b>'.date('l F jS').'</b>');
//]]>
</script>
If you don't do this, and your code contains < or &, it should be rejected by
an XHTML validator.
johnbeech at (not saying) mkv25 dot net 08-Dec-2003 12:42
In the note above about escaping XML/PHP style <?xml tags, the following code
was used:
echo <<<EOD
<?xml version="1.0"?>
...all sorts of XML goes here...
Nothing will affect the output of this code until:
EOD;
?>
$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:
<?xml version="1.0"?>
mypage.php:
<?php
include("xmlheader.txt");
?>
dave at [nospam] dot netready dot biz 18-Mar-2002 12:21
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!
I am moving my site to XHTML and I ran into trouble with the <?xml ?>
interfering with the <?php ?> method of escaping for HTML. A quick check of
the mailing list confirmed that the current preferred method to cleanly output
the <?xml ?> line is to echo it:<br>
<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?>
add a note
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2011 The PHP Group
All rights reserved.