Basic syntax
PHP
tags
When PHP parses
a file, it looks for opening and closing tags, which are <?php
and ?>
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. PHP includes a short echo tag <?=
which is a short-hand to the more
verbose <?php
echo
.
Example
<?php echo 'if you
want to serve PHP code in XHTML or XML documents,
use these tags'; ?>
You can
use the short echo tag to <?= 'print these
values?>.
It's equivalent to <?php echo 'print these
values?>.
<? echo 'this
code is within short tags, but will only work '.
'if short_open_tag is
enabled'; ?>
Short tags
(example three) are available by default but can be disabled either via
the short_open_tag php.ini configuration
file directive, or are disabled by default if PHP is built with the --disable-short-tags configuration.
Note: As short tags can be disabled it is recommended to only use
the normal tags (<?php
?>
and <?= ?>
) to maximize compatibility
If a file
contains only PHP code, it is preferable to omit the PHP closing tag at the end
of the file. This prevents accidental whitespace or new lines being added after
the PHP closing tag, which may cause unwanted effects because PHP will start
output buffering when there is no intention from the programmer to send any
output at that point in the script.
<?php
echo "Hello
world";
// ...
more code
echo "Last
statement";
// the
script ends here with no PHP closing tag
Escaping
from HTML
Everything
outside of a pair of opening and closing tags is ignored by the PHP parser
which allows PHP files to have mixed content. This allows PHP to be embedded in
HTML documents, for example:
<p>This is
ignored by PHP and displayed by the browser. </p>
<?php echo 'While
this is going to be parsed.'; ?>
<p>This is
ignored by PHP and displayed by the browser. </p>
This works as
expected, because when the PHP interpreter hits the?> closing tags, it simply starts
outputting whatever it finds until it hits another opening tag unless in the
middle of a conditional statement in which case the interpreter will determine
the outcome of the conditional before making a decision of what to skip over
Example #1
Advanced escaping using conditions
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
In this example
PHP will skip the blocks where the condition is not met, even though they are
outside of the PHP open/close tags; PHP skips them according to the condition
since the PHP interpreter will jump over blocks contained within a condition
that is not met.
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.
Note: If PHP is embedded within XML or XHTML the normal PHP <?php
?> must be used to remain compliant with the standards.
Instruction
separation
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.
Example showing
the closing tag encompassing the trailing newline.
<?php echo "Hello
World";?>
No newline
<?= "But
newline now" ?>
Note: The closing tag of a PHP block at the end of a file is optional,
and in some cases omitting it is helpful when using include or require,
so unwanted whitespace will not occur at the end of files, and you will still
be able to add headers to the response later. It is also handy if you use
output buffering, and would not like to see added unwanted whitespace at the
end of the parts generated by the included files.
PHP Comments
There are two
ways in which you can add comments to your PHP code. The first turns a single
line (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 into a comment by preceding it with a
pair of forward slashes, like this:
// This
is a comment
You can also
use this type of comment directly after a line of code to describe its action,
like this:
$x -= 5;
// Decrement $x by 1
Multiple-line
comments, there’s a second type of comment, you can use the /* and */ pairs of
characters to open and close comments almost anywhere you like inside your
code. Most, if not all, programmers use this construct to temporarily comment
out entire sections of code that do not work or that, for one reason or another,
they do not wish to be interpreted.
Example
<?php
/* This
is a
multiline
comments
that
will not be
interpreted
*/
?>
Leave a comment
No Cmomments yet