0.3Basic syntax: tags, echo, comments
The essentials in 30 seconds
- Tags to insert PHP into HTML: outside the tags = HTML sent as is, inside = executed PHP.
- echo displays, semicolon terminates: each statement ends with ; — forgetting it is the beginner's mistake #1.
- Document and display short: comments // and /* */, short syntax = ?> in templates.
Running mini-case — Riad writes his first page
Riad, a web developer in Constantine, has his environment ready: he creates bonjour.php in the root folder. He writes HTML with an "island" of PHP between tags to display the visitor's first name with echo, ends each statement with a semicolon, adds a comment to find his way around, and tests the short syntax in his title. He opens the page via localhost: the browser displays the result, never the code. His "edit, run via localhost, read" chain works.
Self-contained case for this section: it returns in the lesson, diagram, script and 48h challenge of section 0.3.
1. Tags and echo
PHP code goes between <?php (opening) and ?> (closing). Everything OUTSIDE these tags is HTML (sent as is to the browser); everything INSIDE is PHP code (executed by the server). That is what makes PHP a natural templating language: <h1>Hello <?php echo 'world'; ?></h1> produces a title with a generated part. echo is the construct for DISPLAYING (producing text/HTML) — your fundamental output tool (there is also print, similar). Each statement ends with a semicolon ; — forgetting it is beginner mistake #1. For a pure-PHP file, the final closing tag is often omitted (avoids stray whitespace). Riad writes his first echo and immediately sees the result via localhost.
2. Comments, short syntax, case
Comments are text ignored by PHP, for explaining the code: // or # for one line, /* ... */ for several lines — essential for readability (by you later, and by others). The short syntax <?= $variable ?>, equivalent to <?php echo $variable; ?>, is handy in HTML templates. Finally case: keywords and functions are not case-sensitive (echo or ECHO work), but variables are ($nom and $Nom are different) — hence the importance of being consistent. Riad comments each block of his page in Constantine and adopts a single case for his variables.
3. Structuring: separating logic and display
A .php file can be mostly HTML with "islands" of PHP (template mode, natural for views) or mostly PHP (logic files). The key principle: separate LOGIC and DISPLAY — process the data first, display afterwards with simple insertions — otherwise you get unreadable "spaghetti code" (the MVC pattern in section 4-1 will formalize this). In views, keep PHP to a minimum (display, loop, condition) and escape displayed data (section 3-4). Riad places his calculations at the top of bonjour.php and his HTML below: his page stays readable.
🔎 Concrete example — 🔎 Concrete example — First script
Mixing PHP and HTML.
Hello
Today is = date("d/m/Y") ?>
Reading: Riad opens the page via localhost: the title displays the first name, the date of the day is generated by PHP.
Lesson: a page where PHP generates the dynamic parts — with the reflex of escaping displayed data (see 3.4).
Section vocabulary
- PHP tags
- PHP code goes between ``; outside the tags = HTML (sent as is), inside = PHP code (executed). Lets you mix PHP and HTML.
Simple example: Riad inserts a dynamic first name in the middle of his HTML title.
What it is not: optional decorations around the code.
Reflex: enter and leave PHP mode as often as needed in a template.
- echo
- Construct for DISPLAYING (producing output): `echo 'Hello';` generates text/HTML in the page. The fundamental output tool (also `print`).
Simple example: Riad displays "Hello, PHP!" and checks the result in the browser.
What it is not: a function that reads user input.
Reflex: display first to check, assume afterwards.
- Semicolon
- Ends each PHP statement (`;`); forgetting it causes a syntax error — the most frequent beginner mistake.
Simple example: Riad forgets a semicolon: the error is reported on the next line, he fixes the previous one.
What it is not: a mere style choice with no consequence.
Reflex: facing a syntax error, check the semicolon on the previous line.
- Comments
- Text ignored by PHP for explaining the code: `//` or `#` (one line), `/* ... */` (several lines); essential for readability.
Simple example: Riad comments the top of bonjour.php to explain what the page generates.
What it is not: text displayed to the visitor.
Reflex: comment the "why", not each obvious line.
- Short syntax = ?>
- Equivalent of ``: `= $variable ?>` displays a variable, handy in HTML templates. Variables are case-sensitive (keywords are not).
Simple example: Riad writes = $prenom ?> in his title instead of a full echo block.
What it is not: a syntax reserved for pure-logic files.
Reflex: in views, prefer = ?> for simple displays.
In practice — In practice — Writing your first PHP code
- Create a .php file in the root folder and insert PHP into HTML with the `` tags.
- Display text with `echo 'Hello, PHP!';` (do not forget the semicolon!) and open the page via localhost.
- Mix HTML and PHP: `
Hello
`; also test the short syntax `= ... ?>`. - Add comments (`//`, `/* ... */`) to document your code, and check that the result displays correctly.
Script ready to say — telling about your first page (30 s)
"I created bonjour.php in the root folder: HTML with an island of PHP between tags that displays the first name with echo. Each statement ends with a semicolon, I commented my code, and via localhost I see the result — never the code."
Say while showing the .php file and the displayed page in the browser side by side.
Copy-paste template — my first PHP page
Hello = $prenom ?>
No variable
Start with a fixed echo ("Hello, PHP!") opened via localhost, then introduce a $prenom variable and the short syntax — one step at a time.
Semicolon hunter
Deliberately remove a semicolon, read the error message, note that it points to the next line, then fix it — mistake #1 will never surprise you again.
Pairing in Constantine
Riad dictates, his colleague types: one reads the .php file aloud (tags, echo, comments), the other describes what the browser displays.
Towards clean
Working page: move all the logic to the top of the file, keep only simple displays in the HTML, and escape each displayed piece of data.
✓ Best practices
- End each statement with a semicolon (trap #1).
- Separate logic (at the top) and display (the HTML afterwards).
- Use = ?> in templates; escape displayed data (3.4).
✗ Common pitfalls
- Forgetting the semicolon or the $ before a variable.
- Anarchically mixing complex logic and HTML (spaghetti code).
- Getting the case of a variable wrong ($nom ≠ $Nom) — silent bug.
Key takeaways
- PHP is inserted into HTML via TAGS ``: outside the tags = HTML (sent as is), inside = PHP (executed). PHP is a natural templating language.
- `echo` DISPLAYS (produces text/HTML): the fundamental output tool. Each statement ends with a SEMICOLON `;` (forgetting it = error, the beginner's trap #1).
- COMMENTS (`//`, `#`, `/* */`) for documenting. Short syntax `= $variable ?>` (= echo) handy in templates.
- KEYWORDS/functions are NOT case-sensitive, but VARIABLES are (`$nom` ≠ `$Nom`) — be consistent. A .php file mixes HTML and "islands" of PHP.
48h challenge — apply tomorrow
Create bonjour.php with Riad's template, open it via localhost, change the first name, and add a comment explaining the page.
- My bonjour.php page displays via localhost with the generated first name.
- I use tags, echo, semicolon and short syntax without help.
- My code is commented and logic is separated from display.
Frequently asked questions
How does PHP mix with HTML, and how should a PHP file be structured?
Everything OUTSIDE the `` tags is HTML (sent as is), everything INSIDE is PHP (executed, its result inserted); you can enter and leave PHP mode as often as you want. Two structures: mostly HTML with islands of PHP ("template" mode, natural for VIEWS — e.g. `= $titre ?>`), or mostly PHP (LOGIC files: processing, functions, classes). Good practices: (1) SEPARATE logic and display — the key principle: logic first, display afterwards, ideally in distinct files (MVC, section 4-1); (2) in VIEWS, keep PHP to a minimum (display, loop, condition); (3) use = ?> for displaying; (4) omit the final closing tag in pure-PHP files; (5) ESCAPE displayed data (XSS security, 3.4).
What are the most frequent syntax mistakes among beginners?
(1) The forgotten SEMICOLON (#1) — the error is often reported on the NEXT line, so check the previous line. (2) The forgotten $ before a variable. (3) Unpaired BRACES/PARENTHESES (often "unexpected end of file") — indenting and a good editor help. (4) Unclosed QUOTES — watch out for apostrophes in a string. (5) Variable CASE (`$nom` ≠ `$Nom`) — silent bug. (6) Confusing = (assignment) and == (comparison). (7) Badly written PHP TAGS. To protect yourself: enable ERROR DISPLAY in development, use VS Code with PHP extensions, indent and test in small increments.
What is the beginner's syntax mistake #1 in PHP?