-
Hajipur, Bihar, 844101
PHP has a simple and flexible syntax, which is one of the reasons it’s beginner-friendly. Understanding PHP syntax is essential because it tells the server how to read and execute your code. Once you’re comfortable with the syntax, writing PHP scripts becomes straightforward.
Every PHP code block starts with <?php
and ends with ?>
. These tags tell the server to treat the enclosed content as PHP code.
<?php
// This is a PHP code block
echo "Hello, PHP Syntax!";
?>
<?php
starts the PHP block
?>
ends it
Any code outside these tags is treated as normal HTML
Tip: You can have multiple PHP blocks in a single file.
PHP is case-sensitive in some areas:
Variable names are case-sensitive: $name
≠ $Name
Function names are not case-sensitive: echo()
, ECHO()
, and Echo()
all work
<?php
$city = "Delhi";
echo $city; // Works
echo $City; // Error: undefined variable
?>
PHP statements are instructions that the server executes. Every statement ends with a semicolon ;
.
<?php
echo "This is a statement."; // Semicolon marks the end
$a = 10; // Assign 10 to variable $a
?>
Tip: Forgetting a semicolon is a common beginner error and causes a parse error.
Comments help explain code but are ignored by the server. PHP supports three types of comments:
<?php
// This is a single-line comment
# Another single-line comment
/*
This is a multi-line comment.
You can write multiple lines here.
*/
?>
Why use comments?
Makes your code easier to read
Helps when debugging or maintaining code
PHP ignores extra spaces, tabs, and line breaks. However, proper indentation makes code readable.
<?php
$number = 5;
if($number > 0) {
echo "Positive number"; // Proper indentation
}
?>
Even though whitespace doesn’t affect execution, it’s good practice for clean code.
PHP works seamlessly with HTML. You can embed PHP blocks inside an HTML page to make your content dynamic.
<!DOCTYPE html>
<html>
<head>
<title>PHP Syntax Example</title>
</head>
<body>
<h1>Welcome!</h1>
<p>
<?php
// Display current year
echo "The year is " . date("Y");
?>
</p>
</body>
</html>
HTML handles the structure
PHP handles dynamic content
This is the basic idea of server-side scripting.
You can write multiple statements inside a PHP block:
<?php
// Assign values
$a = 10;
$b = 20;
// Add two numbers
$sum = $a + $b;
// Display the result
echo "Sum of a and b is: " . $sum;
?>
Each statement ends with a semicolon
You can chain variables and expressions as needed
PHP has two main functions to display output: echo
and print
.
<?php
echo "Hello World!"; // Can display multiple strings
print "Hello PHP!"; // Can display one string and return 1
?>
echo
is slightly faster and can take multiple arguments
print
returns a value (1) so it can be used in expressions
Start with <?php
and end with ?>
for server execution.
End every statement with a semicolon.
Use descriptive variable names.
Follow proper indentation.
Use comments to explain logic.
Separate PHP from HTML for readability when possible.
Example combining all rules:
<!DOCTYPE html>
<html>
<head>
<title>PHP Rules Example</title>
</head>
<body>
<?php
// Define variables
$firstName = "Vicky";
$lastName = "Sanjana";
// Concatenate strings
$fullName = $firstName . " " . $lastName;
// Display full name
echo "<h2>Hello, " . $fullName . "!</h2>";
?>
</body>
</html>
Variables follow naming rules
Statements end with semicolons
Comments explain each part
Always use <?php
and not the short <?
tag to avoid compatibility issues
Use lowercase function names for readability
Keep your code clean and modular
Avoid mixing too much PHP and HTML in large projects — consider templates or frameworks
PHP syntax is straightforward: start with PHP tags, use semicolons, and write readable code with proper indentation and comments. Understanding syntax is essential before moving to variables, operators, or functions. Once you master this, embedding PHP in HTML and building dynamic web pages becomes easy.
Write a PHP script that prints “Learning PHP Syntax!” using the echo
statement and include a single-line comment explaining what the script does.
Create a PHP file that defines two variables, $a = 10
and $b = 5
, then prints their sum with a comment describing each step.
Write a PHP script with multiple statements in a single block: assign a value to a variable, double it, and print the result, including comments for each statement.
Create a PHP page that embeds PHP inside HTML to display “Current Year: 2025” dynamically using the date()
function and include comments explaining the PHP part.
Write a PHP file demonstrating case sensitivity: define $city = "Delhi"
and try printing both $city
and $City
, adding comments to explain the difference.
Create a PHP script using both echo
and print
to display two different messages and add comments explaining when each function is used.
Write a PHP script that uses a multi-line comment to explain what the script does, then define a variable $name = "Vicky"
and print “Hello, Vicky!” dynamically.
Design a PHP script that calculates the area of a rectangle. Use variables $length = 8
and $width = 5
, compute the area, and print it with comments explaining each step.
Create a PHP file with two separate PHP blocks: the first prints “First Block Running” and the second prints “Second Block Running,” adding comments in both blocks.
Write a PHP page combining PHP and HTML to display a greeting:
<h1>Welcome!</h1>
<p>Hello, [Your Name]!</p>
Use a PHP variable for the name and include comments explaining the dynamic content.