0Pricing
PHP Academy · Lesson

PHP Tags and Embedding Syntax

Use opening/closing PHP tags and short echo syntax inside HTML.

PHP Tags and Embedding Syntax is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Mixing PHP and HTML

PHP was designed to embed inside HTML files. When the server processes a .php file, everything between PHP tags is executed; everything else is sent to the browser as-is.

Standard PHP Tags

The standard opening and closing PHP tags:

<!DOCTYPE html>
<html><body>
<?php
    $name = 'Alice';
    echo 'Hello, ' . $name;
?>
<p>This is regular HTML.</p>
<?php
    echo '<p>Back in PHP!</p>';
?>
</body></html>

Short Echo Tag

The short echo tag outputs a value without writing the word echo:

<!-- Long form -->
<h1><?php echo $pageTitle; ?></h1>

<!-- Short form (preferred in templates) -->
<h1><?= $pageTitle ?></h1>

<!-- With escaping -->
<h1><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></h1>

Always Escape Output

Never output user-controlled data without escaping:

<?php
$unsafe = '<script>alert("xss")</script>';
$safe   = htmlspecialchars($unsafe, ENT_QUOTES, 'UTF-8');

// Define a helper:
function e(string $str): string {
    return htmlspecialchars($str, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
?>

<p><?= e($userInput) ?></p>

PHP in HTML Attributes

Embed PHP in HTML attributes — always escape:

<?php
$url    = '/profile/' . $user['id'];
$active = $currentPage === 'home' ? 'active' : '';
$count  = 5;
?>

<a href="<?= htmlspecialchars($url) ?>" class="nav-link <?= $active ?>">
    Inbox <span class="badge"><?= $count ?></span>
</a>

Avoid Short Tags

Short open tags (less-than ? without php) are NOT recommended — they depend on a php.ini setting and conflict with XML. Always use the full opening tag or the short echo form:

<?php
// These are the safe, portable forms:
// <?php echo $name; ?>    — full form
// <?= $name ?>            — short echo (always enabled 5.4+)

// The short open tag <? is different and unreliable
// Do not use it in code intended for multiple environments.

Inline Conditionals in Templates

Use the inline ternary or null coalescing directly in templates:

<?php
$role = 'admin';
$loggedIn = true;
?>

<nav>
    <?= $loggedIn ? 'Welcome back!' : 'Please log in' ?>
    <?php if ($role === 'admin'): ?>
        <a href="/admin">Admin Panel</a>
    <?php endif; ?>
</nav>

Output Buffering for Capture

Capture PHP+HTML output into a variable using output buffering:

<?php
function renderCard(string $title, string $body): string {
    ob_start();
    ?>
    <div class="card">
        <h2><?= htmlspecialchars($title) ?></h2>
        <p><?= htmlspecialchars($body) ?></p>
    </div>
    <?php
    return ob_get_clean();
}

echo renderCard('PHP', 'A server-side scripting language.');

PHP_EOL for Cross-Platform

Use PHP_EOL for the correct line ending on any platform:

<?php
$lines = ['Line 1', 'Line 2', 'Line 3'];
$content = implode(PHP_EOL, $lines);
file_put_contents('output.txt', $content);

// PHP_EOL = "\n" on Linux/Mac, "\r\n" on Windows
echo PHP_EOL === "\n" ? 'Unix line ending' : 'Windows line ending';

Closing Tag Omission

Best practice: omit the closing PHP tag at the end of PHP-only files. This prevents accidental whitespace from being sent before headers:

<?php
// config.php — PHP only, no closing tag needed
defined('APP_KEY') or define('APP_KEY', 'secret123');
define('DB_HOST', 'localhost');
define('DB_NAME', 'myapp');

// No closing PHP tag here — prevents header issues

Multiple PHP Blocks

A file can have multiple PHP blocks interspersed with HTML:

<?php
$items = ['Apple', 'Banana', 'Cherry'];
$count = count($items);
?>

<h2>Shopping List (<?= $count ?> items)</h2>
<ul>
<?php foreach ($items as $item): ?>
    <li><?= htmlspecialchars($item) ?></li>
<?php endforeach; ?>
</ul>

Quick Check

Which PHP output syntax is recommended for echoing a variable in an HTML template?

Recap: PHP Tags and Embedding

Key points:

  • Use <?php ?> for code blocks, <?= ?> for output
  • Always escape output with htmlspecialchars()
  • Omit closing tag at end of PHP-only files
  • Use output buffering to capture rendered HTML
  • Use PHP_EOL for portable line endings

Frequently asked questions

Is the “PHP Tags and Embedding Syntax” lesson free?

Yes — the full text of “PHP Tags and Embedding Syntax” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “PHP Tags and Embedding Syntax”?

Use opening/closing PHP tags and short echo syntax inside HTML. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “PHP Tags and Embedding Syntax” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. PHP Tags and Embedding Syntax
  2. Dynamic HTML with PHP Loops
  3. Including and Requiring Files
  4. Template Pattern: Separating Logic from View
← Back to PHP Academy