|
|
Fejezet 6. Alapvet� szintaxisEscape szekvencia HTML-ben
Amikor a PHP feldolgoz egy f�jlt, akkor egyszer�en a kimenetre m�sol minden
karaktert, am�g nem tal�lkozik valamelyik speci�lis jel�l�ssel (l�sd al�bb!),
amelyet PHP k�dr�szlet kezdetek�nt �rtelmez. Ekkor a PHP futtatni kezdi
a z�r�jel�l�sig tal�lhat� �sszes k�dot. Ezzel a m�k�d�si elvvel lehet k�nnyen
HTML oldalakba elhelyezni PHP k�dot, mivel mindent, ami a PHP nyit�- �s
z�r�jel�l�sein k�v�l esik, teljes m�rt�kben b�k�nhagy, �s csak ezeken bel�li
tartalmat �rtelmezi programk�dk�nt.
N�gyf�le jel�l�ssel lehet az �llom�ny bizonyos r�szeit PHP k�dk�nt
megjel�lni. Ezek k�z�l csak kett� haszn�lhat� b�rmilyen esetben:
<?php. . .?> and <script language="php">. . .</script>
a m�sik kett� ki �s bekapcsolhat� a php.ini
konfigur�ci�s f�jlban. B�r a r�vid vagy az ASP-st�lus� form�k k�nyelmesnek
t�nnek, egyszersmind kev�sb� hordozhat�k, mint a hosszabb v�ltozatok.
Emellett, ha XML vagy XHTML f�jlokba kell PHP k�dot illeszteni, azt csak
<?php. . .?> alakkal lehet - igazodva az XML aj�nl�shoz.
P�lda 6-1. A PHP-m�dba ker�l�s lehet�s�gei 1. <?php echo("Ha XHTML vagy XML dokumentumokat is akarsz szolg�ltatni," .
"biztos szeretni fogod ezt\n"); ?>
2. <? echo ("Ez a legegyszer�bb, egy SGML processing utas�t�s\n"); ?>
<?= $valtozo; # Ez egy r�vid�t�se a "<? echo ..?>"-nak ?>
3. <script language="php">
echo ("N�h�ny szerkeszt� (ilyen pl. a FrontPage) nem" .
"szereti a processing utas�t�sokat");
</script>
4. <% echo ("Haszn�lhatsz ASP-st�lus� tag-eket"); %>
<%= $valtozo; # Ez egy r�vid�t�se a "<% echo ..%>"-nak %> |
|
Az els� lehet�s�g a legink�bb javasolt, mivel ezzel XML-konform dokumentumokban
, mint p�ld�ul XHTML, is lehet PHP k�dokat elhelyezni.
A m�sodik forma nem mindig haszn�lhat�, csak akkor, ha a r�vid nyit�
jel�l�sek enged�lyezve vannak. Enged�lyezhetj�k - PHP 3-ban -
short_tags() f�ggv�nyh�v�ssal, a
short_open_tag
be�ll�t�ssal a PHP konfigur�ci�s f�jlban, vagy a PHP
ford�t�s�n�l a configure program
--enable-short-tags opci�j�val. Annak ellen�re ha alap�rtelmez�s szerint
enged�lyezve van is, a r�vid nyit�jel�l�sek haszn�lata ellenjavallt
A negyedik m�d csak akkor el�rhet�, ha az ASP-st�lus� jel�l�s is
enged�lyezve van az asp_tags
konfigur�ci�s be�ll�t�ssal.
Megjegyz�s: Az ASP-st�lus� tagek a 3.0.4. verzi�t�l haszn�lhat�ak
Megjegyz�s:
A r�vid nyit�jel�l�sek haszn�lata ker�lend� �jrafelhaszn�land�,
sz�les k�r� terjeszt�sre sz�nt k�nyvt�rak vagy programok fejleszt�sekor,
illetve olyan alkalmaz�sok meg�r�sakor, amelyek �zemeltet�se felett a
program �r�j�nak nincs ellen�rz�se, Ennek oka, hogy a r�vid nyit�jel�l�sek
nem minden konfigur�ci�ban haszn�lhat�k, �gy cs�kkenti a hordozhat�s�got.
Hordozhat�, �jrafelhaszn�lhat� komponensek eset�n ne
haszn�ld a r�vid nyit�jel�l�seket!
A lez�r�jel�l�s mag�ban foglalja a jel�l�st k�zvetlen�l k�vet� �jsort
karaktert, ha van ilyen, emellett a PHP blokk utols� sor�t lez�r�
pontosvessz�t (;).
A PHP a k�vetkez� strukt�r�k haszn�lat�t is megengedi:
P�lda 6-2. HTML blokkok felt�telhez k�t�se <?php if ($igazez) { ?>
<strong>Igaz a v�ltoz� tartalma</strong>
<?php } else { ?>
<strong>Hamis a v�ltoz� tartalma</strong>
<?php } ?> |
|
Ez a v�rakoz�soknak megfelel�en m�k�dik, mivel a PHP
?> lez�r�
jel�l�st k�vet�en mindent �tm�sol a kimenetre, am�g egy �jabb nyit�
jel�l�ssel. A fenti p�lda term�sztesen mondvacsin�lt, de nagy m�ret�
sz�vegblokkok ki�rat�sakor hat�konyabbnak bizonyulhat ideiglenesen
elhagyni a PHP m�dot, mint mindezt a sz�veget echo(),
print() vagy ehhez hasonl� f�ggv�nyekkel ki�ratni.
User Contributed Notes Alapvet� szintaxis |
|
[email protected]
03-Jan-2001 08:35 |
|
[Ed Note: this was fixed in 4.0.5 [email protected]]
If you're
using a MacOS text editor, such as BBEdit, for composing your PHP, you'll
want to save your files with Unix line breaks.
This fixes the
"error on line 1" problem where PHP's error system doesn't
recognize the MacOS line breaks properly. It will also allow you more
flexibility in creating multi-line SQL statements.
|
|
11-Oct-2001 06:34 |
|
I use FrontPage 2000 exclusively for creating my PHP documents. I use the
<% %> style syntax. I can put code anywhere, before head (as is
required for cookie data) and anywhere else in between. Using advanced
flow control, i can design an entire page in html, and then put PHP
control statements around it. This is much easier than <% echo %>
commands. You can drop in/out of PHP at ANY time, and the only reason to
ever use <% echo %> is for variables. Save yourself some hassle and
write your html in html and avoid the echo mess.
|
|
[email protected]
12-Dec-2001 06:36 |
|
[Ed Note:
This is because of short_tags, <?xml turns php parsing on,
because of the <?.
[email protected]]
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:
<?php
echo("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"); ?>
|
|
[email protected]
16-Jan-2002 07:40 |
|
Under php 3.0.14 : if you have a line like this : echo "?>
written to file"; And you wish to comment this line, you'll do
this : //echo "?> written to file"; But this will
generate a parse error at the end of script (even if this line was in an
include script).
Why? because '?>' is ignored as long as it is
inside "". But once you've commented, the echo function is
ignored, and '?>' takes its signification : end of script!
I
guess it was corrected on next version, but if you run under php 3.0.14 be
careful, it make me loose a lot of time!
Paquerette
|
|
dave@[nospam].netready.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...
|
|
[email protected]
17-Jun-2002 06:50 |
|
I can't find out how to break the line in the middle of a function. I have
tried the standard Unix '\' continuation trick, but that doesn't work.
|
|
[email protected]
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.
|
|
[email protected]
09-Jul-2002 08:42 |
|
also, if you're using Mac OS X...i highly suggest looking into Dreamweaver
MX. it's been a DREAM in learning and using PHP. no problems with line
breaks. it also colors your code based on the codes type and has pop-up
tips on what goes in certian brackets and functions. really sharp...
|
|
[email protected]
15-Jul-2002 10:37 |
|
Need help how I can build a mysql fot php to get a localhost ??
|
|
15-Jul-2002 03:42 |
|
You don't need the closing tag if you don't plan to add html (or something)
afterwards. This works (but the ; must be there):
<? phpinfo();
|
|
[email protected]
15-Jul-2002 06:45 |
|
Ich habe hier einen Code der mir egentlich sagen m�sste: Ihr letzter
versuch war am... Er funktioniert aber nicht, deshalb gebe ich in hier
rein, falls mir irgendjemand diesen Code richtigstellen kann, sollte er
mir bitte ein mail schicken. Hier der
Code:
<?php $lastvisit =
$http_COOKIE_VARS [,,lastvisit��]; if (!$lastvisit) { echo
,,Sie haben uns in diesen Monat noch nicht
beehrt! ��; } else { echo ,,Ich letzter Besuch war am:
$lastvisit��; } $datum = date(,,d.m.Y
H:i:s��) setcookie(lastvisit��, $datum, time()+30*24*60*60); // 30
tage lang g�ltig ?>
|
|
|
| |