OSPHP.002: Syntax — PHP Tags, Statements, Semicolons, Comments, and Mixing PHP With HTML

A diverse group of developers collaborating around laptops in a warm modern tech workspace.

Elementary Overview

PHP syntax is the set of rules that tells the PHP runtime how source code is written and where PHP code begins and ends. In this lesson, the goal is not to learn variables, arrays, loops, or functions yet. The goal is to become comfortable reading and writing the basic shape of PHP code so later lessons have a clean foundation.

This lesson follows OSPHP.001: PHP Runtime. That lesson proved the runtime works. OSPHP.002 now focuses on the grammar of the source itself: PHP tags, statements, semicolons, comments, whitespace, case sensitivity, short echo syntax, and the way PHP can enter and leave HTML mode inside one file.

What You Should Learn

  • How <?php starts a PHP code section.
  • When ?> is useful and when it is commonly omitted.
  • How statements are separated with semicolons.
  • How echo produces output.
  • How single-line and multi-line comments work.
  • Why whitespace usually improves readability without changing program meaning.
  • What PHP treats as case-sensitive and what it does not.
  • How <?= provides a short form for output.
  • How PHP and HTML can coexist in one document.
  • How to recognize basic parse and syntax errors.

PHP Code Begins With A PHP Tag

The standard opening tag is <?php. A simple file can begin with <?php, followed by PHP statements. The official PHP manual recommends the normal <?php ... ?> form for maximum compatibility and also documents <?= as the short echo form.

A first example can be written as <?php echo "Hello from PHP";. In a PHP-only file, many projects intentionally omit the final ?> closing tag. The PHP manual notes that this helps prevent accidental whitespace or new lines from being sent after the closing tag.

Dani Krossing — a focused beginner lesson on basic PHP syntax and the habits that help prevent common syntax errors.
Two developers collaborating at laptops in a modern server workspace.
Original BitcoinVersus.Tech lesson image for OSPHP.002.

Statements Usually End With Semicolons

PHP programs are built from statements. The PHP manual describes a statement as something such as an assignment, function call, loop, conditional, or another executable instruction. In ordinary PHP syntax, statements are usually terminated with a semicolon.

For example: echo "First line"; and echo "Second line"; are two separate statements. The semicolon tells the parser where one instruction ends before the next instruction begins.

A missing semicolon is one of the easiest beginner errors to create. The parser may report the problem on the next line rather than exactly where the missing character belongs, so always inspect the statement immediately before the reported location as well.

Use echo To Produce Output

echo is a PHP language construct used to send output. A simple statement such as echo "Hello"; produces the text Hello. In a command-line program, that output appears in the terminal. In a web request, that output becomes part of the response returned to the client.

This connects syntax back to the runtime from OSPHP.001: syntax tells PHP how the statement is written, while the runtime actually executes it.

Traversy Media — “PHP For Absolute Beginners.” The course reaches PHP syntax near the beginning and demonstrates how basic statements and output fit into a working PHP environment.

PHP Can Enter And Leave HTML Mode

One of PHP’s defining features is that PHP code can be embedded directly inside an HTML document. The PHP parser processes code inside PHP tags and treats content outside those tags as normal output. That means one file can contain both the document structure taught in OSHTML.001: Document Structure and dynamic PHP output.

A simple mixed example can look like this in normal reading order: <h1>Status</h1>, then <?php echo "Online"; ?>, then more HTML. The browser ultimately receives output; the PHP tags themselves are interpreted on the server side rather than being ordinary page markup.

This ability to move between HTML and PHP is powerful, but it also means you must pay attention to where PHP mode begins and ends. A misplaced opening or closing tag can make source appear as plain output or can trigger a parser error.

r/PHP discussion: developers compare the short echo form <?= ... ?> with normal PHP tags when mixing output and templates.

The Short Echo Tag

PHP provides <?= as shorthand for <?php echo. For example, <?= "Hello" ?> is a compact way to output a value while moving through a template.

Do not confuse <?= with the older configurable short opening form <?. The official PHP manual treats the short echo tag separately and recommends the normal <?php form for ordinary PHP code.

Comments Explain Code Without Executing

Comments let developers leave notes that PHP ignores during normal execution. PHP supports several comment styles. A line can begin with // or #, while a block comment begins with /* and ends with */.

Examples include // explain this line, # another one-line comment, and /* a longer explanation */. Good comments explain intent, assumptions, or unusual behavior. A comment that merely repeats an obvious statement usually adds noise instead of clarity.

The PHP manual also warns that C-style block comments should not be nested. If you open one /* ... */ comment inside another, the first closing marker can terminate the outer comment earlier than you intended.

Giraffe Academy — “Hello World & Setup | PHP | Tutorial 4.” A beginner walkthrough of the first working PHP source and the basic syntax used to produce output.

Whitespace Makes Source Easier To Read

Spaces, blank lines, and indentation usually help humans read PHP without changing the meaning of ordinary statements. For example, echo "Hello"; can sit on its own line, and related statements can be grouped with blank lines around them.

Formatting becomes more important as code grows. A consistent indentation style helps you see where blocks begin and end. The earlier explainer on syntax highlighting covers another editor feature that makes language structure easier to scan.

Case Sensitivity Has Rules

PHP is not uniformly case-sensitive in every part of the language. Variable names are case-sensitive, so later in the curriculum $name and $Name will be different variables. Language keywords and many built-in constructs do not depend on capitalization in the same way.

Even where the language technically accepts multiple capitalizations, consistent lowercase keywords and conventional formatting make code easier to read and easier for teams to review.

PHP-Only Files Commonly Omit The Closing Tag

If a file contains only PHP code, it is common to begin with <?php and omit the final ?>. The official manual explicitly notes this pattern because it avoids accidental trailing whitespace or new lines being sent after PHP finishes.

When PHP is intentionally mixed with HTML, closing a PHP section is useful because it allows normal HTML to resume. The decision therefore depends on the kind of file you are writing.

Read Parse Errors Methodically

  1. Read the first parser error. Later errors may be consequences of the first one.
  2. Inspect the line before the reported line. A missing semicolon or quote can make the parser complain after the real mistake.
  3. Check PHP tags. Make sure PHP mode begins where you expect.
  4. Check quotes. An unmatched quote can turn the rest of the file into an invalid string.
  5. Check delimiters. Parentheses, braces, and brackets need matching partners.
  6. Change one thing at a time. Re-run the file after each correction.

Mini Lab

  1. Create a file named syntax.php.
  2. Start it with <?php.
  3. Add the statement echo "PHP syntax lab\n";.
  4. Run it with php syntax.php.
  5. Add one // comment and one /* ... */ comment.
  6. Remove one semicolon, run the file, and read the parse error.
  7. Restore the semicolon.
  8. Create a second file that contains simple HTML plus one PHP output section.
  9. Replace <?php echo in one simple output location with <?= and compare the result.
  10. For a PHP-only file, remove the final closing tag and verify the script still runs.

Knowledge Check + Answers

  1. What starts a normal PHP code section? <?php.
  2. What usually ends a PHP statement? A semicolon.
  3. What does echo do? It produces output.
  4. Name two one-line comment styles. // and #.
  5. How do block comments begin and end? They begin with /* and end with */.
  6. What does <?= mean? It is shorthand for PHP output using echo.
  7. Can PHP and HTML exist in the same file? Yes. PHP processes code inside PHP tags while content outside those tags passes through as normal output.
  8. Why is the closing tag often omitted in PHP-only files? To reduce the chance of accidental trailing output such as whitespace or new lines.
  9. Are PHP variable names case-sensitive? Yes.

Primary References

Elementary Review

PHP syntax tells the runtime how to read your source. Start PHP code with <?php, separate ordinary statements with semicolons, use comments to explain intent, use <?= for concise output where appropriate, and remember that PHP can enter and leave HTML mode inside one file. When the parser reports an error, inspect tags, quotes, delimiters, and the previous statement before making unrelated changes.

The next PHP curriculum lesson is OSPHP.003: Variables.

Editor’s Note

The featured image is original artwork created specifically for OSPHP.002 and is not reused in the body. The lesson uses a separate original body image and responsive native Gutenberg media embeds.

BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply