PHP Coding Conventions PHP Tags

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

You should always use <?php ?> tags or short-echo tags <?= ?>. Other variations (in particular, short tags <? ?>) should not be used as they are commonly disabled by system administrators.

When a file is not expected to produce output (the entire file is PHP code) the closing ?> syntax should be omitted to avoid unintentional output, which can cause problems when a client parses the document, in particular some browsers fail to recognise the <!DOCTYPE tag and activate Quirks Mode.

Example of a simple PHP script:

<?php

print "Hello World";

Example class definition file:

<?php

class Foo
{
    ...
}

Example of PHP embedded in HTML:

<ul id="nav">
    <?php foreach ($navItems as $navItem): ?>
        <li><a href="<?= htmlspecialchars($navItem->url) ?>">
            <?= htmlspecialchars($navItem->label) ?>
        </a></li>
   <?php endforeach; ?>
</ul>


Got any PHP Question?