Lesson 4.2. PHP tags


HTML and PHP languages being nested within the same file, it was necessary to define a syntax allowing to dissociate them during processing :

When the file is a pure PHP script (without display or HTML) it is preferable not to place the closing tag at the end of the file to avoid unwanted displays in the client's browser.

Short tags

The short tags <?= $Variable?> display the output of a code or the content of a variable without the need of `echo function. Here is a example:

<!-- Both line do the same disply -->
<?php echo 'PHP tag<br>'; ?>
<?= 'PHP tag<br>' ?> 

In practice, the use of short tags is preferred (when suited) to avoid burdening the source codes.


The following code creates three PHP varaiables and displays a table with three rows. Use short tags to display the variable content in each row of the table:

$firstRow="This text must be placed in the first row";
$secondRow="This text must be placed in the second row";
$thirdRow="This text must be placed in the third row";

<table border="1">
  <tr><td> </td></tr>
  <tr><td> </td></tr>
  <tr><td> </td></tr>

Here is the expected result:

Expected output of short tag exercice

See also

Last update : 09/17/2022