PHP Tags
PHP code must live between <?php and ?> tags. Everything outside is treated as plain HTML and sent to the browser as-is.
<h1>This is plain HTML</h1>
<?php
echo "This is PHP output";
?>
<p>More plain HTML</p>
<?= "Short echo PHP" ?>
Three tag styles
| Tag | Use | Recommended? |
|---|---|---|
<?php ... ?> | Standard PHP block | Yes (always) |
<?= ... ?> | Short echo - prints a single expression | Yes (always available) |
<? ... ?> | Short tag - requires short_open_tag=On | No (removed in PHP 8) |
For files that contain only PHP, omit the closing ?>. This avoids accidental whitespace being sent before HTTP headers, which causes the dreaded "headers already sent" error.
Statements & Semicolons
Every PHP statement ends with a semicolon ;. A statement is one complete instruction.
<?php
$name = "Ruban"; // assignment statement
echo $name; // output statement
echo "Hello"; echo $name;// multiple statements on one line - legal but ugly
// You can split a long statement across multiple lines
$message = "This is a "
. "long string "
. "split into lines";
The line does not matter to PHP - the semicolon does. Forget a semicolon and you get a parse error pointing to the next line.
Comments
PHP supports three styles of comments. Use them generously - your future self will thank you.
<?php
// Single-line comment (C++ style) - most common
# Single-line comment (shell style) - rarely used
/*
Multi-line comment
spans across multiple lines
*/
/**
* Doc comment (PHPDoc) - used to document
* functions, classes, and methods.
*
* @param string $name The user name
* @return string Welcome message
*/
function welcome(string $name): string {
return "Hello, $name";
}
The /** ... */ style is read by IDEs (VS Code, PhpStorm) for autocomplete and type hints, and by tools like phpDocumentor for generating API docs. Always document public functions and classes this way.
Case Sensitivity Rules
PHP has unusual case-sensitivity rules. Memorize them now to save future debugging:
| Element | Case-sensitive? | Example |
|---|---|---|
| Variables | Yes | $name ≠ $Name |
| Constants (defined) | Yes | API_KEY ≠ api_key |
| Function names | No | echo() = ECHO() |
| Method names | No | $obj->save() = $obj->SAVE() |
| Class names | No | new User() = new USER() |
| Keywords (if, echo, while) | No | IF = if |
Even though PHP allows ECHO and While, real codebases always use lowercase keywords (echo, while), camelCase methods, PascalCase classes, and UPPER_SNAKE_CASE constants. Match the convention.
Whitespace & Line Breaks
PHP ignores whitespace between tokens. These all work identically:
<?php
$total = 10 + 5;
$total=10+5;
$total
=
10
+
5
;
Always use the readable version. Most teams follow PSR-12 which mandates 4-space indentation, no tabs.
Alternative Syntax for HTML
When mixing PHP into HTML, curly braces become hard to track. PHP provides an alternative syntax using : and end* keywords:
<ul>
<?php foreach ($users as $user): ?>
<li>
<?= htmlspecialchars($user["name"]) ?>
<?php if ($user["admin"]): ?>
<strong>(Admin)</strong>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
Each control structure has an end keyword: endif, endfor, endforeach, endwhile, endswitch.
Language Constructs vs Functions
PHP has two kinds of callable things, and they look almost identical:
<?php
// Language constructs - parentheses are optional
echo "Hello";
echo("Hello"); // works
print "Hello";
include "file.php";
// Real functions - parentheses required
strlen("Hello");
date("Y-m-d");
Language constructs (echo, print, include, require, isset, unset, empty) are part of the language grammar, so they don't need parentheses. Real functions always need them.
Syntax Best Practices
- Always use
<?php, never<? - Omit the closing
?>in pure PHP files - Use 4 spaces for indentation (PSR-12), never tabs
- One statement per line - your diff tool and your eyes will thank you
- Lowercase keywords -
if,while,true,null - PascalCase classes, camelCase methods, snake_case variables/files
- Comment the why, not the what - the code shows what, comments explain why
Next Steps
The syntax foundations are in place. Now move on to working with data: