PHP: �򥻻y�k - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<�y���Ѧ����O���j>
view the version of this page
Last updated: Sat, 22 May 2004

�� 5. �򥻻y�k

�q HTML ���h�X

�� PHP ��R�@���ɮ׮ɡA���O���|��@��r���@���B�z�A���D���J�W�F�䤤�@�ӯS����ҡA�Щ� PHP �{�����}�l�C�o�ɡAPHP �~�}�l�H PHP �{���X�Ӹ�Ķ�Ӭq��r�A�ܨ쥦�J�W PHP �{���X��������ҡC�������аO�H�᪺��r�S�|�Q��@�O���q��r�ӳB�z�F�C�o�سB�z��k���z�������b HTML ��󤤴O�J PHP ���{���X�CPHP ���ҥH�����N��@�O PHP �{���X�ӭ�R�B���ҥH�~���h��ʤ��ʡC

�ΨӪ�� PHP �{���X�϶�����Ҧ@���|�M�A����u�� <?php. . .?> �M <script language="php">. . .</script> ��M�O�g�`�i�H�ϥΪ��C�t�~��M (<?. . . ?> �� <script language="php">. . .</script>) �i�H�q�L�]�w php.ini �ɨӶ}���C���M�u�����M ASP �������Ҩϥΰ_�Ӹ�����K�A���b�ݮe�W���̫o�l�פ񤣤W�������C�Ҧp�A�p�G�z�n�b XML �� XHTML ���O�J PHP�{���X�A���z�N�@�w�n�ϥ� <?php. . .?> �������ҥH�ŦX XML ���зǤF�C

PHP �䴩�����ҥ]�A�G

�d�� 5-1. �h�X HTML ���覡

1.  <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

2.  <? echo ("this is the simplest, an SGML processing instruction\n"); ?>
    <?= expression ?> This is a shortcut for "<? echo expression ?>"
  
3.  <script language="php">
      
echo ("some editors (like FrontPage) don't
             like processing instructions"
);
  
</script>

4.  <% echo ("You may optionally use ASP-style tags"); %>
   <%= $variable; # This is a shortcut for "<% echo . . ." %>

�Ĥ@�ؤ�k (<?php. . .?>) �O�Q��ij�ϥΪ��A�]�����i�H���z�b�ŦX XML �зǪ�XHTML �X���O�J PHP �{���X�C

�ĤG�ؤ�k�u���b�ҰʥH��~�i�H�ϥΡA�Ӷ}�Ҫ��覡�]�A�G�ϥ� short_tags() ��� (�u�� PHP 3 �Ӥw)�A�b PHP �պA�ɤ��}�� short_open_tag�A�Φb�sĶ PHP �ɦb configure �[�J -enable-short-tag �ﶵ�C�Y�Ϧb php.ini-dist �ɤ��Q�w�]���}�ҡA�u���������٬O���Q��ij�ϥΪ��C

�ĥ|�� ASP �������ҥu���b�պA�ɤ��}�ҤF asp_tags ��~��ϥΡC

�`: ASP �����Ҫ��䴩�b 3.0.4 �����~�[�J���C

�`: �b�}�o�j���{���B�s�g�ΨӦA���t���{���w�A�αN�z���{�����p�b�O�H�����A���W�ɡA�ڭ̫�ij�z�ϥΪ����� PHP ���ҡA�]���ëD�Ҧ������A�����}�ҤF�u���Ҫ��䴩�C

�{���϶�������аO�N�]�A���H��᪺����r�����Y�����ܡ��C���~�A�����аO�w�]�t�F���� (;)�A�ҥH�z�����A�����[�W�C

PHP ���\�z�ϥΦp�U���g�k�G

�d�� 5-2. �i���h�X�d��

<?php
if ($expression) {
  
?>
    <strong>This is true.</strong>
   <?php
} else {
  
?>
    <strong>This is false.</strong>
   <?php
}
?>
�W�z�{���X�N�p�`���B�@�A�]���� PHP �J�W ?> �����аO�ɡA���N���[�B�z����X�Ҧ���r�A����J�W�t�@�Ӷ}�l���ҡC��z���{���ݭn��X�j�q����r�ɡA�ϥΤW�z�h�X��k��X��r��IJv�|��ϥ� echo() �� print()() ��ƨӱo�󰪡C



add a note add a note User Contributed Notes
�򥻻y�k
thorgil at comhem dot se
01-Jun-2004 01:38
note on <script language="php">...</script>
the language attribute is as we all know, not legal with html4 or xhtml1.
use the "type" attribute instead.
/T
crtrue at coastal dot edu
01-May-2004 05: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):

<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.
mart3862 at yahoo dot com dot au
18-Apr-2004 03: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:

<?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 ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'

In other words, it explicitly forbids the characters ?> 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.

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.
george at whiffen dot net
26-Feb-2004 01:12
Tip: New lines immediately following a close tag, (?>,</script>) are suppressed in the output. 

For example:

Contact:-
Name:<?= $myname?>
Telephone: <?= $myphone?>

will typically produce:

Contact:-
Name: My NameTelephone: My Phone

To get the new line back, just add anything after the close tag and before the new line.  A single space character works fine.
Anon
22-Feb-2004 12:05
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).
TarquinWJ
06-Feb-2004 12: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 ?>

<?xml version="1.0" encoding="UTF-8"?>

To avoid the problem, simply replace <? with <<? ?>?
and ?> with ?<? ?>>

<<? ?>?xml version="1.0" encoding="UTF-8"?<? ?>>

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 11: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 &lt; and &amp; 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
07-Dec-2003 10:42
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:
<?xml version="1.0"?>

mypage.php:
<?php
  include("xmlheader.txt");
?>
thunder at cumt dot net
26-Aug-2003 03:46
I user this for outputting my xhtml head:
<?php
    
echo <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "">
<html xmlns="" lang="en-US"
xml:lang="en-US">
EOD;?>
nd at mad dot scientist dot com
14-Feb-2003 10:07
As for the "an _SGML_ processing instruction is actually written as <?php ..>, i.e. without a trailing question mark." issue I rather prefer <xsl:text>?</xsl:text> at the very end of the <xsl:processing-instruction> element. This nicely outputs a question mark first and then the closing bracket, XML style ("?>").

Be sure to include the <xsl:text> element, otherwise the parser may add additional whitespace after the question mark.

Andreas
de \ kibo \ niels
19-Dec-2002 12:19
A follow-up to the first posting on this page: If you're writing PHP code on a non-UNIX system like MacOS (and even on a UNIX system like Mac OS X!), be very careful about line breaks.

Although this was apparently fixed in 4.0.5, I got the weirdest errors while testing PHP scripts that I wrote in BBEdit (6.5) on MacOS X (10.2, running Apache 1.3.27 with PHP 4.1.2).

The Mac-style line breaks (\r) that BBEdit usually produces by default confuse the PHP parser, and it will report the strangest parse errors. (I even got errors in lines of code that were commented out! This drove me nuts for days).

Your documents have to use UNIX-style linebreaks (\n) to prevent these errors. It's advisable to configure BBEdit to produce UNIX line breaks by default in every new document (preferences!).

This problem possibly also occurs with DOS/Win systems whose linebreaks are \r\n.
stodden at in dot tum dot de
18-Jun-2002 08:51
if you're experiencing problems with php PIs when generating  creating mixed php/html content with e.g. an XSLT processor in html output mode:
it's not the processors fault.
an _SGML_ processing instruction is actually written as <?php ..>, i.e. without a trailing question mark.

<
xsl:processing-instruction name="php">
  echo
$hello;
</
xsl:processing-instruction>
will therefor not not work like it should.

a sane solution to work around this is generating <script> tags instead.
dave at [nospam] dot netready dot biz
18-Mar-2002 10: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!

(
These examples only give a warning with error_reporting(E_ALL) )

<?
PHP/*<Some HTML>*/?> fails...
<?/*<Some HTML>*/?> works...
mrtidy at mail dot com
12-Dec-2001 06:36
[Ed Note:
This is because of short_tags, <?xml turns php parsing on, because of the <?.
--
irc-html@php.net]

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"); ?>

<�y���Ѧ����O���j>
 Last updated: Sat, 22 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Thu Jun 10 10:22:01 2004 CEST