PHP Syntax – Tags, echo and Comments
In this lesson: Learn basic PHP syntax — opening tags, echo vs print, comments, statements and semicolons — with output. Hindi & English.
PHP syntax basics
Syntax means the rules for writing code that PHP can understand. The good news: PHP's rules are few and simple. Code goes inside PHP tags, each statement ends with a semicolon, and you can mix PHP with HTML freely. Get these basics right and everything else builds on top.
<?php, end each line with ;, close with ?>.PHP tags and semicolons
<?php
$name = "Aman";
echo $name;
?>
Everything between <?php and ?> is PHP. Each instruction (statement) ends with a semicolon ;. Missing that semicolon is the single most common beginner error.
echo and print
Both send text to the page. echo is the everyday choice.
<?php
echo "Hello ", "World"; // echo can take several values
print "Bye"; // print takes one
?>
echo can output several comma-separated values and is slightly faster; print outputs one value. Use echo unless you have a reason not to.
Mixing PHP with HTML
You can jump in and out of PHP inside a page.
<h1>Welcome</h1>
<p>Today is <?php echo date("l"); ?></p>
Today is Monday
The HTML is sent as-is, and the PHP block is replaced by its output. This mixing is exactly what makes web pages dynamic.
Comments
<?php
// single-line comment
# also a single-line comment
/* a multi-line
comment */
echo "Comments are ignored by PHP";
?>
Comments explain your code to humans and are skipped by PHP. Use them to note why you did something, not just what.
Common mistakes
- Missing semicolon. The number one beginner error — every statement needs a
;. - Unclosed PHP tag or quote. An open
"or missing?>breaks the whole page. - Wrong file extension. Mixed PHP/HTML must be saved as
.php. - Case slips in variables.
$Nameis not$name— keep casing consistent.
Summary
- PHP code lives inside
<?php ... ?>tags. - Every statement ends with a semicolon
;. echooutputs text (and takes several values);printtakes one.- PHP mixes freely with HTML in a
.phpfile. - Comment with
//,#or/* ... */.
Next, store data in PHP variables, or output dynamic values with PHP data types.
Frequently Asked Questions
Why does every PHP statement end with a semicolon?
; tells PHP where one statement ends and the next begins. Forgetting it is the most common beginner error and causes a "syntax error, unexpected..." message. Every statement needs one.What is the difference between echo and print?
echo is slightly faster and can output several things separated by commas. print can only output one thing and returns a value of 1. For everyday use, echo is the common choice.Can I write PHP inside an HTML file?
.php extension. You open a PHP block with <?php, write code, and close it with ?>, then continue with HTML. This mixing is a core PHP feature.What are the ways to write comments in PHP?
// or # for a single-line comment, and /* ... */ for a multi-line comment. Comments are ignored by PHP and are there to explain the code to humans.Is PHP case-sensitive?
$name and $Name differ), but keywords and function names like echo, if and strlen are not. To stay safe, keep your casing consistent.