PHP 8.2.0 RC 6 available for testing

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 #1 PHP Opening and Closing Tags

1.  <?php echo 'if you want to serve PHP code in XHTML or XML documents,
                use these tags'
?>

2.  You can use the short echo tag to <?= 'print this string' ?>.
    It's equivalent to <?php echo 'print this string' ?>.

3.  <? 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 maximise 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

add a note

User Contributed Notes 1 note

up
-36
anisgazig at gmail dot com
1 year ago
If you want your file to be interpreted as php then your file must start and end with <?php and ?> and everything outside of that is ignored by the php parser.

<?php
php code
..//parsed
php code..//parsed
?>
hellow..//normal test but ignred by php parser

Three types of tag are available in php
1.normal tag(<?php ?>)
2.short echo tag(<?= ?>)
3.short tag(<? ?>)

short tag are bydefault available but can be disabled by short_open_tag = Off and also disabled bydefault if php will  built with --disabe--short--tags()

As short tag can be disabled so only use the normal and short echo tag.

If your file only have php code then  do not use closing tag.
<?php
//php code;
//php code;
//php code;

but if you are embedding php with html then enclose php code with opening and closing tag.
<
html>
<
head>
</
head>
<
body>
<?
php
//php code;
//php code;
//php code;

?>
</body>
</html>

If you want to just print single text or something ,you should use shorthand version .<?= $var ?>

But if you want to process something, you should use normal tag.
<?php
       
//$var = 3;
        //$var2 = 2;
        //$var3 = $var+$var2;
        //if($var3){//result}

?>

If you embedded php with html and single line, do not need to use semicolon
<html>
<head>
<body>
<?= $var ?>
</body>
</head>
</html>
but if you have multiple line, then use semicolon.
<?php
//line 1;
//line 2;
//line 3;
?>
To Top