Getting Started Lesson 3 of 17
PHP Syntax — echo, Comments and Mixing PHP with HTML
The basic rules of PHP — tags, statements and semicolons, echo and print, comments, case sensitivity, and the clean ways to mix PHP with HTML.
This lesson covers the small rules every PHP file follows, and the best ways to combine PHP with HTML so your pages stay readable.
Tags and statements
<?php
echo "Hello! ";
echo "Each statement ends with a semicolon.";
Output
Hello! Each statement ends with a semicolon.
A missing semicolon gives an error like "syntax error, unexpected token". The line number in the message is often the line after the missing semicolon.
echo and print
<?php
echo "Line 1<br>";
echo "Several ", "values ", "at once<br>";
print "print works too<br>";
echo 10 + 5;
Output
Line 1<br>Several values at once<br>print works too<br>15
echo is the usual choice. To make new lines in the browser, output HTML like <br> or <p>; a \n only adds a line break in the page source.
Comments
<?php
// single-line comment
# also a single-line comment
/*
multi-line comment
*/
echo "Comments are ignored.";
Case sensitivity
- Variables are case-sensitive:
$nameand$Nameare different. - Keywords and function names are not:
echo,ECHOandEchoall work (but always write them lowercase).
Mixing PHP and HTML
You can switch in and out of PHP anywhere in a page:
<?php $school = "Rizal National High School"; $students = 1240; ?>
<!DOCTYPE html>
<html>
<body>
<h1><?php echo $school; ?></h1>
<p>We have <?= $students ?> students this school year.</p>
</body>
</html>
HTML output
<!DOCTYPE html> <html> <body> <h1>Rizal National High School</h1> <p>We have 1240 students this school year.</p> </body> </html>
<?= $x ?> is a short form of <?php echo $x; ?>. It's always available in modern PHP and keeps templates tidy.
Loops inside HTML
For HTML-heavy pages, PHP has an alternative syntax with : and endforeach/endif that reads better than braces:
<?php $subjects = ["Math", "Science", "English"]; ?>
<ul>
<?php foreach ($subjects as $s): ?>
<li><?= $s ?></li>
<?php endforeach; ?>
</ul>
HTML output
<ul> <li>Math</li> <li>Science</li> <li>English</li> </ul>
Building HTML with echo
For small pieces, echoing HTML is fine. Double-quoted strings can include variables directly:
<?php
$name = "Ana";
$grade = 95;
echo "<p class=\"result\">$name got $grade</p>";
echo '<p class="result">' . $name . ' got ' . $grade . '</p>';
Output
<p class="result">Ana got 95</p><p class="result">Ana got 95</p>
The dot . joins strings in PHP (not +). You'll learn more in the Strings lesson.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04