What is the correct & common way to start and finish a PHP block of code?
The correct and recommended way is the standard tags: <?php to start and ?> to finish.
<?php
echo "Hello from standard tags";
?>
Output:
Tutorials Class - Output Window
Hello from standard tags
There is also the short echo tag <?= ... ?>. It prints a value and it is always available (PHP 5.4 and above), so it is good inside HTML.
<p>Year: <?= date("Y") ?></p>
- The old short open tag
<? ... ?>works only whenshort_open_tagis On in php.ini. It is usually Off, so do not use it. - In a file that has only PHP code, leave out the closing
?>tag. It avoids problems with extra spaces after it.
See also PHP Syntax.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Syntax |
| Exercises & Assignments |
|---|
| Simple Tips for PHP Beginners |
| Experiment with Basic PHP Syntax Errors |
| Using phpinfo() – Display PHP Configuration & Modules |
| Interview Questions & Answers |
|---|
| What is the correct & common way to start and finish a PHP block of code? |
| What is the default extension of the PHP file? |