Introduction
PHP is one of the most popular server-side scripting languages used for web development. It is widely used to create dynamic and interactive web pages.
Today, more than 70% of websites that use server-side programming rely on PHP in some form. Every PHP file has a .php extension and is executed on the server before being sent to the browser.
What is PHP Syntax?
PHP Syntax refers to the set of rules that define how PHP programs are written and structured.
PHP also has syntax rules that must be followed to write correct code. These rules include:
- How to write PHP tags
- How to print output
- How to embed PHP inside HTML
- How to use statements and functions
PHP code is always written inside special tags and executed on the server.
Why it is Used
1. Easy to Learn
PHP syntax is simple compared to other programming languages, making it beginner-friendly.
2. Dynamic Web Development
It allows developers to create dynamic content like forms, login systems, and dashboards.
3. HTML Integration
PHP can be easily embedded inside HTML, making it very flexible.
4. Server-Side Execution
PHP code runs on the server, improving security and performance.
5. Wide Usage
Used in websites, CMS platforms, and web applications.
Syntax
1. Standard PHP Syntax
Every PHP script starts with <?php and ends with ?>.
<?php
?>
2. Basic Example
Let’s print a simple message using PHP:
<?php
print 'Hello PHP';
?>
Output:
Note: In this example:
- print is used to display output
- The text is written inside quotes
3. Using echo (Alternative)
PHP also provides echo to print output:
<?php
echo "Hello PHP";
?>
Both echo and print are used for displaying output.
4. PHP File Extension
Every PHP file must have a .php extension.
index.php
test.php
Examples:
Example 1: Simple PHP Program
<?php
echo "Welcome to PHP Syntax Tutorial";
?>
Example 2: Multiple Statements
<?php
echo "Hello ";
echo "World!";
?>
Output:
HTML Embedding in PHP File
One of the best features of PHP is that it can be embedded directly inside HTML.
Example:
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1>Hello techfliez.com</h1>
<?php
print "Hi, how are you?";
?>
</body>
</html>
Output:
- HTML content is displayed normally
- PHP code runs on the server and outputs:
Note: This feature makes PHP very powerful for creating dynamic websites.
PHP Short Tags
PHP also provides a shorter syntax for writing PHP code.
Short Tag Syntax:
<?
?>
How to Enable PHP Short Tag?
By default, short tags may be disabled. You can enable them by editing the php.ini file.
Steps:
1. Open the php.ini file
2. Find the line:
short_open_tag=Off
3. Change it to:
short_open_tag=On
4. Restart your server
Example Using Short Tag:
<?
print 'Hello Friends';
?>
Output:
Real-Life Example
Let’s understand PHP syntax with a real-world scenario.
Example: Dynamic Greeting Page
<?php
$name = "John";
echo "Hello " . $name . ", Welcome to our website!";
?>
Output:
Note: Here:
- PHP dynamically displays the user’s name
- This is commonly used in dashboards and login systems
Common Mistakes
1. Missing PHP Tags
❌ Wrong:
echo "Hello";
✔️ Correct:
<?php
echo "Hello";
?>
2. Missing Semicolon
❌ Wrong:
echo "Hello"
✔️ Correct:
echo "Hello";
3. Using HTML File Instead of PHP
PHP code will not run in .html files.
4. Incorrect Quotes
Mixing quotes incorrectly can cause errors.
5. Overusing Short Tags
Short tags may not work on all servers, so it’s better to use standard syntax.
Conclusion
PHP syntax is simple, flexible, and easy to learn, making it an excellent starting point for beginners in web development.
Although PHP provides short tags for convenience, it is always recommended to use the standard syntax for better compatibility. Avoid common mistakes such as missing semicolons or incorrect file extensions to ensure smooth execution.
PHP Syntax – Interview Questions
Q 1: How does a PHP script start and end?
Q 2: Is semicolon mandatory in PHP?
Q 3: Is PHP case-sensitive?
Q 4: Can PHP be embedded in HTML?
Q 5: What is the use of echo in PHP?
PHP Syntax – Objective Questions (MCQs)
Q1. PHP statements end with ______.
Q2. PHP code starts with ______.
Q3. PHP variables start with ______.
Q4. Which is correct syntax to print text in PHP?
Q5. PHP is ______ case sensitive.