Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 03

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.

💡 The three basics: open with <?php, end each line with ;, close with ?>.

PHP tags and semicolons

<?php
  $name = "Aman";
  echo $name;
?>
Aman

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
?>
Hello WorldBye

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>
Welcome
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 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. $Name is not $name — keep casing consistent.

Summary

  • PHP code lives inside <?php ... ?> tags.
  • Every statement ends with a semicolon ;.
  • echo outputs text (and takes several values); print takes one.
  • PHP mixes freely with HTML in a .php file.
  • 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?
The 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?
Both output text. 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?
Yes, as long as the file has a .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?
Use // 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?
Partly. Variable names are 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.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicVariables in PHP Your progress: 3 / 50 (6%) · Lesson 4 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free