PHP Syntax, echo & Comments
The basic rules: PHP tags, printing output, statements and comments.
What you will learn
- Write PHP inside <?php ?> tags
- Output with echo
- Add comments and end statements
The PHP tags
All PHP code lives between <?php and ?>. Anything outside those tags is sent to the page untouched; anything inside is run by PHP. Every statement (instruction) ends with a semicolon ; — think of it as the full stop at the end of a sentence.
<?php
echo "Hello, World!";
echo "<br>";
echo "Learning PHP at CodingClave";
?>Reading it line by line: the first echo prints the text Hello, World!. The second prints <br>, which is the HTML tag for a line break, so the next text starts on a new line. The third prints Learning PHP at CodingClave. Each line ends with a semicolon.
Note: Output (in the browser):
Hello, World!
Learning PHP at CodingClave
The <br> does not appear as text — the browser reads it as "go to a new line", which is why the two messages sit on separate lines.
echo and print
echo outputs text (or HTML) to the page. You can output plain text, variables, or HTML tags — it is the command you will use more than any other in PHP. (print does almost the same thing; echo is the common choice.)
Comments
A comment is a note for humans that PHP ignores when it runs. Use comments to explain *why* code does something. PHP gives you three styles:
<?php
// This is a single-line comment
# This is also a single-line comment
/* This is a
multi-line comment */
echo "Comments are ignored by PHP";
?>Note: Output (in the browser):
Comments are ignored by PHP
None of the comment lines appear — PHP skips them entirely. // and # each comment out a single line; /* ... */ comments out everything between the marks, even across several lines. Only the echo actually runs.
Watch out: Forgetting the semicolon ; at the end of a statement is the #1 beginner error — PHP will show a “syntax error”. Check your semicolons first.
Q. What does echo do in PHP?
✍️ Practice
- Echo three lines of text, separated by
<br>. - Add all three comment styles to a file and confirm they do not show.
🏠 Homework
- Build a PHP page that outputs a complete HTML card (heading + paragraph) using
echo.