agyanadda@gmail.com

PHP Syntax and Comments

PHP Basics 1 min read Updated Dec 1, 2026 Topic 3 of 36

PHP tags

PHP code goes between <?php and ?>:

<?php
echo 'Hello';
?>

In files that contain only PHP, leave out the closing ?> — it avoids accidental spaces in the output.

Statements

  • Every statement ends with a semicolon ;
  • Code blocks use curly braces { }

Case sensitivity

  • Variables are case-sensitive: $name and $Name are different
  • Keywords and function names are not: echo, ECHO both work (but use lowercase)

Mixing PHP and HTML

You can switch between HTML and PHP anywhere. The short echo tag prints a value: <?= $name ?>

Comments

// single-line comment
# also single-line
/* multi-line
   comment */

Example

Example — PHP inside HTML PHP
<?php
$student = 'Asha';
$marks = 86; // out of 100
/*
  Decide the result
*/
$result = $marks >= 33 ? 'Pass' : 'Fail';
?>
<!DOCTYPE html>
<html>
<body>
  <h2>Result card</h2>
  <p>Name: <?= $student ?></p>
  <p>Marks: <?= $marks ?></p>
  <p>Result: <strong><?= $result ?></strong></p>
</body>
</html>

Output: Result card / Name: Asha / Marks: 86 / Result: Pass

Test Yourself With Exercises

Exercise 1 Multiple Choice

How does a PHP code block start?

Exercise 2 True / False

In PHP, $name and $Name are the same variable.

Exercise 3 Fill in the Blank

Each PHP statement ends with a ___.

Frequently Asked Questions

PHP could not read your code — usually a missing semicolon, quote or bracket on that line or the line before it.