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 Introduction |
| PHP Terminology |
| PHP Installation |
| PHP Syntax |
| PHP Comments |
| PHP exit() Function |
| Exercises & Assignments |
|---|
| Write a program to print “Hello World” using echo |
| Simple Tips for PHP Beginners |
| Experiment with Basic PHP Syntax Errors |
| Using phpinfo() – Display PHP Configuration & Modules |