PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousStruttura del LinguaggioSeparazione delle istruzioninext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

Capitolo 6. Sintassi Fondamentale

Modi per uscire dalla modalit� HTML

Quando il PHP inizia a manipolare un file, produrr� in uscita solamente il testo che trova. Cos� se si ha un file HTML, e si modifica l'estensione in .php, il file continuer� ad essere visibile.

Se si vogliono inserire delle istruzioni PHP in un certo punto del file, occorre indicarlo al php, entrando nella "modalit� PHP" in uno dei seguenti modi:

Esempio 6-1. Metodi per uscire dalla modalit� HTML

1.  <? echo ("questo � il pi� semplice, ovvero come istruzione SGML\n"); ?>
    <?= espressione ?>  Questa � un'abbreviazione per "<? echo espressione ?>"
 
2.  <?php echo("se si vogliono produrre documenti XHTML o XML, si utilizzi questo modo\n"); ?>

3.  <script language="php"> 
        echo ("alcuni editor (tipo FrontPage) non 
               amano le istruzioni di elaborazione");
    </script>

4.  <% echo ("Opzionalmente puoi utilizzare tag nello stile ASP"); %>
    <%= $variable; # Questo � una abbreviazione per "<%echo .." %>

Il primo � disponibile solo se sono stati abilitati i tags abbreviati. Ci� pu� essere impostato abilitando nel file di configurazione del PHP l'opzione short_open_tag, oppure compilando il PHP utilizzando l'opzione --enable-short-tags nel comando configure.

Il secondo modo � il metodo generalmente preferito, in quanto consente alla prossima generazione di XHTML di essere facilmente implementato con il PHP.

Il quarto modo � disponibile solo se sono stati attivati nel file di configurazione i tag in stile ASP tramite l'opzione asp_tags.

Nota: Il supporto per i tag nello stile ASP � stato aggiunto nella versione 3.0.4.

I tag di chiusura del blocco includono, se presente, il carattere di newline immediatamente sucessivo.

PHP allows you to use structures like this:

Esempio 6-2. Advanced escaping

<?php

if (boolean-expression) {
    ?>
<strong>This is true.</strong>
    <?php
} else {
    ?>
<strong>This is false.</strong>
    <?php
}
    ?>
This works as expected, because PHP handles text within ?> and <?php as an echo() statement.

User Contributed Notes
Sintassi Fondamentale
add a note about notes
[email protected]
03-Jan-2001 09: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 07: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 07: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 08: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 11: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 07: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 09: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 09: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 11:37

Need help how I can build a mysql fot php to get a localhost ??
15-Jul-2002 04: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 07: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
?>

add a note about notes
previousStruttura del LinguaggioSeparazione delle istruzioninext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Thu Sep 5 20:07:53 2002 CEST