learn php script Headline Animator

jeudi 8 janvier 2009

Basic syntax

Escaping from HTML

When
PHP parses a file, it looks for opening and closing tags, which tell
PHP to start and stop interpreting the code between them. Parsing in
this manner allows php to be embedded in all sorts of different
documents, as everything outside of a pair of opening and closing tags
is ignored by the PHP parser. Most of the time you will see php
embedded in HTML documents, as in this example.

This is going to be ignored.


echo 'While this is going to be parsed.'; ?>

This will also be ignored.

You can also use more advanced structures:

????? 10-1. Advanced escaping

if ($expression) {
?>
This is true.
} else {
?>
This is false.
}
?>
This
works as expected, because when PHP hits the ?> closing tags, it
simply starts outputting whatever it finds until it hits another
opening tag. The example given here is contrived, of course, but for
outputting large blocks of text, dropping out of PHP parsing mode is
generally more efficient than sending all of the text through echo() or print().

There are four different pairs of opening and closing tags which can be used in php. Two of those, and , are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.

????: Also
note that if you are embedding PHP within XML or XHTML you will need to
use the tags to remain compliant with standards.

????? 10-2. PHP Opening and Closing Tags

1. echo 'if you want to serve XHTML or XML documents, do like this'; ?>

2.

3. echo 'this is the simplest, an SGML processing instruction'; ?>
This is a shortcut for "echo expression ?>"

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

While
the tags seen in examples one and two are both always available,
example one is the most commonly used, and recommended, of the two.

Short tags (example three) are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if php was configured with the --enable-short-tags option.

????: If you are using PHP 3 you may also enable short tags via the short_tags() function. This is only available in PHP 3!

ASP style tags (example four) are only available when they are enabled via the asp_tags php.ini configuration file directive.

????: Support for ASP tags was added in 3.0.4.

????: Using
short tags should be avoided when developing applications or libraries
that are meant for redistribution, or deployment on PHP servers which
are not under your control, because short tags may not be supported on
the target server. For portable, redistributable code, be sure not to
use short tags.


As in C or
Perl, PHP requires instructions to be terminated with a semicolon at
the end of each statement. The closing tag of a block of PHP code
automatically implies a semicolon; you do not need to have a semicolon
terminating the last line of a PHP block. The closing tag for the block
will include the immediately trailing newline if one is present.

echo 'This is a test';
?>

echo 'This is a test' ?>

????: The
closing tag of a PHP block at the end of a file is optional, and in
some cases not using it is helpful when using output buffering and include() or require().


Comments

PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. For example:

echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo
'One Final Test'; # This is a shell-style comment
?>

The
"one-line" comment styles only comment to the end of the line or the
current block of PHP code, whichever comes first. This means that HTML
code after // ?> WILL be printed: ?> skips out of the PHP mode and returns to HTML mode, and // cannot influence that. If asp_tags configuration directive is enabled, it behaves the same with // %>. However, tag doesn't escape PHP mode in one-line comment.

This is an # echo 'simple';?> example.


The header above will say 'This is an example'.

'C' style comments end by the first encountered */. You should be careful not to nest 'C' style comments, which can happen when commenting out large blocks.

/*
echo 'This is a test'; /* This comment will cause a problem */
*/
?>


Aucun commentaire:

Enregistrer un commentaire