PHP Syntax & Basic Structure

Introduction – Understanding PHP Syntax

Just like English has grammar rules, every programming language has syntax — the set of rules that defines how code must be written.

In PHP, the syntax is simple and beginner-friendly, which is one reason why it’s such a popular language for web development.

PHP code is executed on the server, and the output is sent to the browser as plain HTML.

How PHP Works

When a browser requests a .php file:

  1. The request goes to the web server (like Apache).

  2. The PHP engine processes all PHP code on the server.

  3. The server sends the output (HTML) to the browser.

  4. The user sees the result — not the PHP code itself.

PHP Script Structure – The Building Blocks

Every PHP script follows a basic structure.

PHP Script Format

 
<?php // PHP code goes here ?>

 Explanation:

  • <?php → Opening tag to start PHP code

  • ?> → Closing tag to end PHP code

  • Everything inside these tags is treated as PHP code

 Note: PHP files are saved with the .php extension (e.g., index.php).

Example – A Simple PHP Program

Let’s write our first PHP script.

 
<?php echo "Hello, PHP World!"; ?>

 Output:

 
Hello, PHP World!

 Explanation:

  • echo is a PHP statement used to print text or output to the browser.

  • Every statement in PHP ends with a semicolon (;).

Mixing PHP with HTML

One of PHP’s biggest advantages is that it can be embedded directly inside HTML.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>My First PHP Page</title>
</head>
<body>
  <h1><?php echo "Welcome to PHP Programming!"; ?></h1>
</body>
</html>

Output in Browser:

Welcome to PHP Programming!

Here, the PHP code runs on the server and only sends the HTML result (<h1>Welcome to PHP Programming!</h1>) to the browser.

PHP Comments

Comments are lines ignored by the PHP engine — they’re used to explain code and make it easier to understand.

Single-line Comments

 
// This is a single-line comment # This is also a single-line comment

Multi-line Comments

 
/* This is a multi-line comment. It can span multiple lines. */

Best Practice: Always comment your code to explain logic, especially in projects and assignments.

Case Sensitivity in PHP

PHP is partially case-sensitive:

  • Variable names are case-sensitive ($Name and $name are different).

  • Keywords and function names are not case-sensitive (echo and ECHO both work).

Example:

<?php
$Name = "Arvinder";
echo $Name; // Works
echo $name; // Error (undefined variable)
?>

PHP Variables – The Basics

Variables in PHP are used to store data (like text or numbers).
They always start with a $ (dollar sign).

Example:

<?php
$name = "John";
$age = 25;

echo "My name is $name and I am $age years old.";
?>

Output:

 
My name is John and I am 25 years old.

PHP Statements and Semicolons

Each PHP statement must end with a semicolon (;).
If you forget it, the program will throw an error.

Correct:

 
echo "Hello PHP!"; echo "Welcome to programming.";

Incorrect:

 
echo "Hello PHP!" echo "Welcome to programming."

 This will cause a syntax error.

PHP Whitespace and Line Breaks

PHP ignores extra spaces and line breaks, but using proper indentation makes your code more readable.

Example:

<?php
echo "PHP";
    echo "is easy";
        echo "to learn!";
?>

This code still works fine, but always keep clean formatting.

PHP Escape Sequences

To handle quotes or special characters inside strings, use escape sequences.

Example:

 
<?php echo "He said, \"PHP is great!\""; ?>

Output:

 
He said, "PHP is great!"

PHP Tags Variations

Although the standard tag <?php ... ?> is recommended, PHP supports other types of tags:

Tag TypeExampleUsage
Standard Tag<?php ... ?> Recommended
Short Tag<? ... ?> Not always supported (depends on server)
ASP Style Tag<% ... %> Deprecated
Script Tag<script language="php"> ... </script> Rarely used

Always prefer standard tags to ensure compatibility across all servers.

Common Syntax Errors

Some common mistakes PHP beginners make:

ErrorReason
Missing semicolon (;)Statement not properly ended
Misspelled variable namesPHP is case-sensitive
Forgetting quotes around stringsStrings must be enclosed in ' ' or " "
Missing PHP tagsCode won’t execute without <?php ... ?>

Always test your code step-by-step and check for errors in your browser.

PHP Code Execution Flow

Here’s how the PHP execution process works:

  1. Browser requests a .php file.

  2. Server sends it to the PHP interpreter.

  3. PHP executes all code inside <?php ?>.

  4. The output (HTML) is sent back to the browser.

 This is why users can never see your PHP code — only the output!