Lesson 3 of 50 – PHP Syntax
6%

PHP Syntax

PHP syntax refers to the rules used to write PHP programs. In this lesson, you will learn PHP tags, statements, semicolons, comments, case sensitivity, variables, and how to embed PHP code inside HTML.

Note: PHP code is generally written between the <?php opening tag and the ?> closing tag. A PHP file commonly uses the .php extension.
Basic Structure of PHP

A basic PHP program starts with the PHP opening tag. You can write PHP statements between the opening and closing tags.

Example:
<?php
echo "Hello World!";
?>
Output:
Hello World!

The echo statement displays text. The PHP interpreter executes the code and sends the resulting output to the browser.

PHP Opening and Closing Tags

The PHP opening tag tells the interpreter where PHP code begins. The closing tag marks the end of that PHP code block.

Tag Purpose
<?php Starts a PHP code block.
?> Ends a PHP code block when a closing tag is used.
Example:
<?php
echo "Welcome to PHP";
?>

In a file containing only PHP code, it is common to omit the closing ?> tag to avoid accidental output from trailing whitespace.

PHP Statements and Semicolons

A PHP statement is an instruction that the interpreter executes. Most simple PHP statements must end with a semicolon (;).

Example:
<?php
echo "My Name is Rahul";
echo "I am learning PHP";
?>
Output:
My Name is RahulI am learning PHP

Both statements execute in sequence. Use an HTML line break when you want the messages displayed on separate lines.

Example with Line Break:
<?php
echo "My Name is Rahul";
echo "<br>";
echo "I am learning PHP";
?>
Output:
My Name is Rahul
I am learning PHP
PHP Comments

Comments explain the code and are ignored by the PHP interpreter. PHP supports single-line and multi-line comments.

Single-Line Comments:
<?php
// This is a single-line comment
echo "Hello PHP";
# This is also a single-line comment
?>
Multi-Line Comments:
<?php
/*
This is a multi-line comment.
It can contain multiple lines.
*/
echo "Learning PHP";
?>
Output:
Hello PHPLearning PHP

Comments help make programs easier to read and maintain.

PHP is Case-Sensitive

PHP variable names are case-sensitive. This means variables with different capitalization are treated as different variables.

Example:
<?php
$name = "Rahul";
$Name = "Amit";

echo $name;
echo "<br>";
echo $Name;
?>
Output:
Rahul
Amit

Here, $name and $Name are two different variables.

Remember: PHP variable names are case-sensitive. PHP keywords and many built-in function names are generally case-insensitive, but using their standard lowercase spelling is recommended.
Writing PHP Inside HTML

PHP can be embedded inside an HTML document. The server executes the PHP code and returns the generated output along with the HTML.

Example:
<!DOCTYPE html>
<html>
<head>
    <title>PHP Syntax</title>
</head>
<body>

    <h1>Welcome to My Website</h1>

    <?php
    echo "<p>This message comes from PHP.</p>";
    ?>

</body>
</html>
Output:

Welcome to My Website

This message comes from PHP.

Save this code in a file with the .php extension and run it through a PHP-enabled web server.

PHP Variables in Statements

Variables store values that can be used in PHP statements. A PHP variable starts with a dollar sign ($), followed by its name.

Example:
<?php
$name = "Rahul";
$age = 20;

echo "My name is " . $name;
echo "<br>";
echo "My age is " . $age;
?>
Output:
My name is Rahul
My age is 20

The dot (.) operator joins strings and values together in PHP.

PHP Whitespace and Indentation

Spaces, tabs, and line breaks help make PHP code easier to read. PHP generally ignores extra whitespace between tokens when it does not change the program's meaning.

Example:
<?php
$name = "Rahul";
echo $name;
?>

The following code also works, but it is harder to read:

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

Use consistent indentation and one statement per line to make your code easier to understand and maintain.

Key Points
  • PHP code commonly starts with <?php.
  • The closing tag is ?>.
  • Most simple PHP statements end with a semicolon.
  • Use // or # for single-line comments.
  • Use /* ... */ for multi-line comments.
  • PHP variable names are case-sensitive.
  • PHP code can be embedded in HTML.
  • Good indentation makes code easier to read.

🧠 Quick Quiz

1. Which tag is used to start a PHP code block?




2. Which symbol usually ends a PHP statement?




3. Which comment symbol starts a single-line comment in PHP?