PHP Syntax & Comments
This page covers the core grammar of PHP: tags, statements, comments, output with echo / print,
and how to embed PHP in HTML. Mastering these basics will make the rest of the tutorial easy.
PHP Tags
PHP code starts with <?php and ends with ?>. Anything outside runs as normal HTML.
<!DOCTYPE html>
<html>
<body>
<?php
// PHP block
echo "Hello from PHP!";
?>
</body>
</html>
<? ... ?> depends on short_open_tag and might be disabled on servers. Always use <?php ... ?>.Statements & Semicolons
Each PHP statement ends with a semicolon (;). Forgetting it usually causes a parse error.
<?php
echo "Line 1";
echo "Line 2";
?>
Case Sensitivity
- Keywords (like
echo,if,while) are case-insensitive. - Variables are case-sensitive (
$name≠$Name). - Function names are case-insensitive (by convention, use one style).
<?php
$name = "Sonu";
ECHO "Hello, " . $name . "!"; // OK
// echo $Name; // Notice/Undefined variable if used
?>
Comments
| Type | Syntax | Use |
|---|---|---|
| Single-line | // comment | Quick notes |
| Single-line | # comment | Shell-style |
| Multi-line | /* comment */ | Block / documentation |
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multi-line comment
Useful for longer notes
*/
?>
Output: echo vs print
| Construct | Returns | Notes |
|---|---|---|
echo | Nothing | Faster; can output multiple expressions separated by commas |
print | 1 | Behaves like a function; slightly slower; single argument |
<?php
echo "A", "B", "C";
print "Hello with print";
?>
echo in most cases for simplicity and speed.Mixing PHP with HTML
Common in templates: use PHP to inject dynamic values into HTML.
<!DOCTYPE html>
<html>
<body>
<?php $title = "Welcome"; ?>
<h1><?php echo $title; ?></h1>
<p>Today is
<strong><?php echo date("l"); ?></strong>.
</p>
</body>
</html>
Escaping HTML
When displaying user input, escape it to avoid XSS:
<?php
$name = $_GET['name'] ?? 'Guest';
$safe = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
echo "Hello, " . $safe . "!";
?>
Whitespace & Newlines
PHP mostly ignores whitespace, so format your code for readability. HTML controls visual spacing in the browser.
Strings: Quotes, Escapes, HEREDOC / NOWDOC
Single quotes do not parse variables; double quotes do.
<?php
$name = "Sonu";
echo 'Hello $name'; // prints: Hello $name
echo "Hello $name"; // prints: Hello Sonu
?>
HEREDOC (parses variables) and NOWDOC (no parsing) are great for multi-line text:
<?php
$name = "Sonu";
$heredoc = <<<TXT
Hello $name
This is HEREDOC.
TXT;
$nowdoc = <<<'TXT'
Hello $name
This is NOWDOC.
TXT;
echo $heredoc;
echo $nowdoc;
?>
Basic Operators (Preview)
We’ll cover operators in detail later. Here’s a quick preview:
| Type | Examples |
|---|---|
| Assignment | =, +=, -=, .= |
| Arithmetic | +, -, *, /, % |
| Comparison | ==, ===, !=, <>, <, >, <=, >= |
| Logical | &&, ||, ! |
| String | . (concatenation) |
Practice Tasks
- Print your name and today’s date on the same line using
echo. - Create a paragraph with HTML that includes a PHP-generated number (e.g., random 1–100).
- Write a HEREDOC that contains a small multi-line message with a variable inside.