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.
<?php opening tag and the ?> closing tag. A PHP file commonly uses the .php extension.
A basic PHP program starts with the PHP opening tag. You can write PHP statements between the opening and closing tags.
<?php
echo "Hello World!";
?>
The echo statement displays text. The PHP interpreter executes the code and sends the resulting output to the browser.
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. |
<?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.
A PHP statement is an instruction that the interpreter executes. Most simple PHP statements must end with a semicolon (;).
<?php
echo "My Name is Rahul";
echo "I am learning PHP";
?>
Both statements execute in sequence. Use an HTML line break when you want the messages displayed on separate lines.
<?php
echo "My Name is Rahul";
echo "<br>";
echo "I am learning PHP";
?>
Comments explain the code and are ignored by the PHP interpreter. PHP supports single-line and multi-line comments.
<?php
// This is a single-line comment
echo "Hello PHP";
# This is also a single-line comment
?>
<?php
/*
This is a multi-line comment.
It can contain multiple lines.
*/
echo "Learning PHP";
?>
Comments help make programs easier to read and maintain.
PHP variable names are case-sensitive. This means variables with different capitalization are treated as different variables.
<?php
$name = "Rahul";
$Name = "Amit";
echo $name;
echo "<br>";
echo $Name;
?>
Here, $name and $Name are two different variables.
PHP can be embedded inside an HTML document. The server executes the PHP code and returns the generated output along with the HTML.
<!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>
This message comes from PHP.
Save this code in a file with the .php extension and run it through a PHP-enabled web server.
Variables store values that can be used in PHP statements. A PHP variable starts with a dollar sign ($), followed by its name.
<?php
$name = "Rahul";
$age = 20;
echo "My name is " . $name;
echo "<br>";
echo "My age is " . $age;
?>
The dot (.) operator joins strings and values together in PHP.
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.
<?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.
<?php.?>.// or # for single-line comments./* ... */ for multi-line comments.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?