PHP Syntax and Basic Structure for Beginners

PHP is a popular server-side programming language used to build dynamic websites and web applications.

WordPress, content management systems, e-commerce websites, login systems, dashboards, and database-driven applications can all be developed using PHP.

Before learning advanced PHP concepts, you must understand the basic syntax and structure of a PHP program.

In this lesson, you will learn:

  • How to write PHP code
  • How PHP opening and closing tags work
  • How to display output
  • How to write comments
  • How statements and semicolons work
  • How to combine PHP with HTML
  • Basic PHP naming rules
  • Common beginner mistakes
  • Important interview questions
  • Simple coding problems with solutions

What Is PHP Syntax?

PHP syntax means the rules you must follow when writing PHP code.

Every programming language has its own syntax. These rules tell the computer how to understand and execute the program.

For example, a simple PHP program looks like this:

<?php

echo "Hello, World!";

?>

This code displays:

Hello, World!

PHP code normally starts with:

<?php

It may end with:

?>

However, in a file that contains only PHP code, the closing tag is usually omitted.

Recommended style:

<?php

echo "Hello, World!";

Table of Contents

Basic Structure of a PHP Program

A basic PHP program contains three main parts:

<?php

echo "Welcome to PHP";

?>

Let us understand each part.

1. PHP Opening Tag

<?php

This tells the server that PHP code starts here.

Anything written after this tag is processed as PHP until PHP is closed or the file ends.

2. PHP Statement

echo "Welcome to PHP";

This statement displays text in the browser.

3. PHP Closing Tag

?>

This tells the server that the PHP code has ended.

The closing tag is optional when the file contains only PHP.

Your First PHP Program

Create a file named:

index.php

Add the following code:

<?php

echo "My first PHP program";

When you run the file through a PHP server, the browser displays:

My first PHP program

Explanation

<?php

Starts the PHP code.

echo

Sends output to the browser.

"My first PHP program"

This is a string. A string is text written inside quotation marks.

;

The semicolon ends the PHP statement.

PHP Statements

A PHP statement is an instruction that tells PHP to perform an action.

Example:

<?php

echo "Hello";
echo "Welcome to PHP";

This code contains two statements.

PHP executes the statements from top to bottom.

Output:

HelloWelcome to PHP

There is no space between the two outputs because we did not add one.

A better version is:

<?php

echo "Hello ";
echo "Welcome to PHP";

Output:

Hello Welcome to PHP

You can also use an HTML line break:

<?php

echo "Hello<br>";
echo "Welcome to PHP";

Output:

Hello
Welcome to PHP

Why Do PHP Statements End with a Semicolon?

Most PHP statements end with a semicolon:

;

Example:

<?php

echo "Learning PHP";

The semicolon tells PHP that the current instruction has ended.

Incorrect code:

<?php

echo "Learning PHP"
echo "Next lesson";

This produces a syntax error because the first statement does not have a semicolon.

Correct code:

<?php

echo "Learning PHP";
echo "Next lesson";

Displaying Output with echo

The echo statement is commonly used to display text, numbers, variables, and HTML.

Displaying text

<?php

echo "Hello from PHP";

Output:

Hello from PHP

Displaying a number

<?php

echo 100;

Output:

100

Numbers do not require quotation marks.

Displaying HTML

<?php

echo "<h2>Welcome to My Website</h2>";
echo "<p>This paragraph was generated with PHP.</p>";

PHP sends the HTML to the browser, and the browser displays it as formatted content.

Displaying multiple values

<?php

echo "PHP", " is", " easy", " to learn.";

Output:

PHP is easy to learn.

Displaying Output with print

PHP also provides the print statement.

Example:

<?php

print "Hello from PHP";

Output:

Hello from PHP

Both echo and print can display output.

Difference between echo and print

echo can output multiple values:

<?php

echo "PHP", " Full-Stack", " Developer";

print accepts only one value:

<?php

print "PHP Full-Stack Developer";

print returns the value 1, while echo does not return a value.

For normal beginner projects, echo is used more frequently.

PHP Is Case-Sensitive

PHP is partly case-sensitive.

PHP keywords such as echo, if, and while are generally not case-sensitive.

These examples work:

<?php

echo "Hello";
ECHO "World";

However, variable names are case-sensitive.

Example:

<?php

$name = "Ibrahim";

echo $name;
echo $Name;

$name and $Name are considered different variables.

The second variable has not been defined, so PHP may show a warning.

Recommended style:

<?php

$name = "Ibrahim";

echo $name;

Always use consistent lowercase naming for variables.

PHP Comments

Comments are notes written inside code.

PHP ignores comments when the program runs.

Comments help developers explain code and make it easier to understand.

Single-line comment using //

<?php

// Display a welcome message
echo "Welcome to PHP";

Single-line comment using #

<?php

# This is also a single-line comment
echo "Learning PHP";

Multi-line comment

<?php

/*
This program displays
a simple welcome message
for PHP beginners.
*/

echo "Welcome";

Why comments are useful

Comments can explain:

  • What a section of code does
  • Why a particular solution was used
  • What still needs to be completed
  • How another developer should use the code

Avoid comments that simply repeat the code.

Weak comment:

<?php

// Echo hello
echo "Hello";

Better comment:

<?php

// Display the welcome message on the home page
echo "Hello";

Writing PHP Inside HTML

One of PHP’s main strengths is that it can be added directly inside HTML.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Example</title>
</head>
<body>

    <h1>My PHP Website</h1>

    <p>
        <?php echo "This text was generated with PHP."; ?>
    </p>

</body>
</html>

Explanation

The browser reads the normal HTML.

When the server reaches:

<?php echo "This text was generated with PHP."; ?>

PHP processes the code and sends the result to the browser.

The final HTML received by the browser is similar to:

<p>This text was generated with PHP.</p>

A Cleaner PHP and HTML Example

<?php

$pageTitle = "PHP Beginner Tutorial";
$message = "Welcome to your first PHP lesson.";

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo $pageTitle; ?></title>
</head>
<body>

    <h1><?php echo $pageTitle; ?></h1>

    <p><?php echo $message; ?></p>

</body>
</html>

Explanation

First, two PHP variables are created:

$pageTitle = "PHP Beginner Tutorial";
$message = "Welcome to your first PHP lesson.";

Then those values are displayed inside the HTML:

<title><?php echo $pageTitle; ?></title>

and:

<p><?php echo $message; ?></p>

This is how PHP makes a web page dynamic.

PHP Short Echo Syntax

PHP provides a shorter way to display a value.

Normal syntax:

<?php echo $pageTitle; ?>

Short echo syntax:

<?= $pageTitle ?>

Example:

<?php

$name = "Ibrahim";

?>

<h1>Welcome, <?= $name ?></h1>

Output:

Welcome, Ibrahim

The short echo syntax is very useful in PHP templates.

PHP Whitespace

PHP usually ignores extra spaces, tabs, and blank lines.

These examples produce the same result:

<?php
echo "Hello";
<?php

echo     "Hello";

However, clean formatting is important for readability.

Recommended:

<?php

echo "Hello";
echo "Welcome to PHP";

Not recommended:

<?php echo"Hello";echo"Welcome to PHP";

Both may work, but the second version is difficult to read and maintain.

PHP File Extension

PHP files normally use the .php extension.

Examples:

index.php
about.php
login.php
register.php
dashboard.php

Do not save PHP code inside an .html file unless your server has been specially configured to process PHP in HTML files.

Correct:

index.php

Incorrect for normal PHP execution:

index.html

PHP Naming Rules

Although variables are covered more deeply in the next lesson, you should know the basic naming rules.

A PHP variable starts with a dollar sign:

$name = "Ibrahim";

Valid examples:

$userName = "Ibrahim";
$age = 30;
$total_price = 99;

Invalid examples:

$2name = "Ibrahim";
$user-name = "Ibrahim";

A variable name:

  • Must begin with a letter or underscore after $
  • Cannot begin with a number
  • Cannot contain spaces
  • Cannot contain a hyphen
  • Is case-sensitive

Recommended PHP Code Style

Use readable formatting.

Recommended:

<?php

$name = "Ibrahim";
$course = "PHP Full-Stack Development";

echo "Student: " . $name;
echo "<br>";
echo "Course: " . $course;

Avoid writing everything on one line:

<?php $name="Ibrahim";$course="PHP";echo $name;echo $course;

Good code should be easy for both you and other developers to understand.

Concatenation in PHP

Concatenation means joining strings together.

PHP uses the dot operator:

.

Example:

<?php

$firstName = "Ibrahim";
$lastName = "Khalil";

echo $firstName . " " . $lastName;

Output:

Ibrahim Khalil

Explanation

$firstName

Contains the first name.

" "

Adds a space.

$lastName

Contains the last name.

The dot joins all three values.

Double Quotes and Single Quotes

PHP supports both double and single quotation marks.

Double quotes

<?php

$name = "Ibrahim";

echo "Hello, $name";

Output:

Hello, Ibrahim

PHP can read a variable inside double quotes.

Single quotes

<?php

$name = "Ibrahim";

echo 'Hello, $name';

Output:

Hello, $name

PHP does not normally replace variables inside single quotes.

To use a variable with single quotes, concatenate it:

<?php

$name = "Ibrahim";

echo 'Hello, ' . $name;

Output:

Hello, Ibrahim

Escape Characters

Sometimes you need to include quotation marks inside a string.

Incorrect:

<?php

echo "He said "Hello"";

Correct using escape characters:

<?php

echo "He said \"Hello\"";

Output:

He said "Hello"

You can also use single quotes outside:

<?php

echo 'He said "Hello"';

Common PHP Syntax Errors

1. Missing semicolon

Incorrect:

<?php

echo "Hello"

Correct:

<?php

echo "Hello";

2. Missing quotation mark

Incorrect:

<?php

echo "Hello;

Correct:

<?php

echo "Hello";

3. Wrong variable capitalisation

Incorrect:

<?php

$name = "Ibrahim";

echo $Name;

Correct:

<?php

$name = "Ibrahim";

echo $name;

4. Using an invalid file extension

Incorrect:

index.html

Correct:

index.php

5. Opening the PHP file directly

Opening this in the browser may not execute PHP correctly:

C:\xampp\htdocs\project\index.php

Use a local server URL instead:

http://localhost/project/index.php

Complete Beginner Example

<?php

// Store basic information
$name = "Ibrahim";
$role = "PHP Full-Stack Developer";
$website = "ibrahimx.com";

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title><?= $name ?> - Developer Profile</title>
</head>
<body>

    <h1>Welcome to My Profile</h1>

    <p>
        My name is <?= $name ?>.
    </p>

    <p>
        I am learning to become a <?= $role ?>.
    </p>

    <p>
        Visit my website:
        <strong><?= $website ?></strong>
    </p>

</body>
</html>

Explanation

The PHP section creates three variables:

$name = "Ibrahim";
$role = "PHP Full-Stack Developer";
$website = "ibrahimx.com";

The values are then displayed inside HTML using:

<?= $name ?>

This example demonstrates how PHP can create dynamic content inside an HTML page.

Interview Questions and Answers

1. What is PHP?

PHP is a server-side scripting language mainly used to develop dynamic websites and web applications.

The PHP code runs on the server, and the generated output is sent to the browser.

2. What does PHP stand for?

PHP currently stands for:

PHP: Hypertext Preprocessor

It is a recursive acronym.

3. How does PHP code begin?

PHP code usually begins with:

<?php

4. Is the PHP closing tag required?

No. The closing tag is optional in files that contain only PHP code.

Recommended:

<?php

echo "Hello";

Omitting the closing tag can prevent accidental whitespace from being sent to the browser.

5. Why does a PHP statement use a semicolon?

The semicolon marks the end of a PHP statement.

Example:

echo "Hello";

6. What is the difference between echo and print?

Both display output.

echo:

  • Can accept multiple values
  • Does not return a value
  • Is commonly used

print:

  • Accepts one value
  • Returns 1

7. Is PHP case-sensitive?

PHP variable names are case-sensitive.

$name

and:

$Name

are different variables.

PHP keywords such as echo are not generally case-sensitive, but lowercase keywords are recommended.

8. Can PHP be written inside HTML?

Yes. PHP can be embedded directly inside an HTML document.

Example:

<h1><?php echo "Welcome"; ?></h1>

9. What is the short echo syntax?

The short echo syntax is:

<?= $value ?>

It is equivalent to:

<?php echo $value; ?>

10. What is string concatenation in PHP?

Concatenation means joining strings together.

PHP uses a dot:

echo "Hello " . "World";

11. What file extension is used for PHP?

PHP files normally use:

.php

12. Where does PHP execute?

PHP executes on the web server.

The browser receives the generated output, usually HTML.

13. What is a syntax error?

A syntax error happens when the code breaks PHP language rules.

Examples include:

  • Missing semicolon
  • Missing quotation mark
  • Incorrect bracket
  • Invalid code structure

14. What are comments in PHP?

Comments are notes inside code that PHP does not execute.

Examples:

// Single-line comment
/*
Multi-line comment
*/

15. Why is PHP called a server-side language?

PHP runs on the server rather than directly inside the user’s browser.

The server processes the PHP and sends the result to the browser.

Coding Problems with Solutions

Problem 1: Display a Welcome Message

Task

Write a PHP program that displays:

Welcome to PHP Programming

Solution

<?php

echo "Welcome to PHP Programming";

Explanation

The echo statement sends the text to the browser.

The text is written inside double quotation marks because it is a string.

The semicolon ends the statement.

Problem 2: Display Two Lines

Task

Display:

My name is Ibrahim
I am learning PHP

Solution

<?php

echo "My name is Ibrahim<br>";
echo "I am learning PHP";

Explanation

The first echo displays the first sentence.

The HTML tag:

<br>

creates a line break.

The second echo displays the next sentence.

Problem 3: Display a Heading with PHP

Task

Use PHP to generate an HTML heading.

Solution

<?php

echo "<h1>PHP Beginner Course</h1>";

Explanation

PHP sends the <h1> HTML element to the browser.

The browser then displays the text as a large heading.

Problem 4: Join a First Name and Last Name

Task

Create two variables and display the complete name.

Solution

<?php

$firstName = "Ibrahim";
$lastName = "Khalil";

echo $firstName . " " . $lastName;

Output

Ibrahim Khalil

Explanation

The dot operator joins strings.

The empty space:

" "

is added between the first name and last name.

Problem 5: Create a Simple Profile

Task

Display a name, profession, and country.

Solution

<?php

$name = "Ibrahim";
$profession = "Web Developer";
$country = "Germany";

echo "Name: " . $name;
echo "<br>";

echo "Profession: " . $profession;
echo "<br>";

echo "Country: " . $country;

Explanation

Three variables store the information.

Concatenation joins each label with its variable.

The <br> tags place the information on separate lines.

Problem 6: Use PHP Inside HTML

Task

Create an HTML page and use PHP to display a page title.

Solution

<?php

$pageTitle = "My PHP Website";

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?= $pageTitle ?></title>
</head>
<body>

    <h1><?= $pageTitle ?></h1>

</body>
</html>

Explanation

The variable is created before the HTML begins.

The short echo syntax displays the variable inside the <title> and <h1> elements.

Problem 7: Fix the Syntax Error

Incorrect code

<?php

echo "Hello PHP"
echo "Welcome";

Correct solution

<?php

echo "Hello PHP";
echo "Welcome";

Explanation

The first statement was missing a semicolon.

Every echo statement must be properly ended.

Problem 8: Display Quotation Marks

Task

Display:

PHP is a "server-side" language.

Solution

<?php

echo 'PHP is a "server-side" language.';

Explanation

The outer string uses single quotation marks.

This allows double quotation marks to appear inside the string without escaping them.

Another valid solution is:

<?php

echo "PHP is a \"server-side\" language.";

Problem 9: Use a Comment

Task

Create a PHP program containing a comment that explains the output.

Solution

<?php

// Display the main course title
echo "PHP Full-Stack Development";

Explanation

The comment explains the purpose of the next statement.

PHP ignores the comment during execution.

Problem 10: Build a Small Course Card

Task

Display a course name, lesson number, and learning status using PHP and HTML.

Solution

<?php

$courseName = "PHP Full-Stack Development";
$lessonNumber = 1;
$status = "In Progress";

?>

<div class="course-card">

    <h2><?= $courseName ?></h2>

    <p>Lesson: <?= $lessonNumber ?></p>

    <p>Status: <?= $status ?></p>

</div>

Explanation

PHP stores the dynamic values in variables.

The short echo syntax inserts each value inside the HTML.

Later, the same structure could display information from a database.

Practice Exercises

Try solving these exercises before looking at any solution.

Exercise 1

Write PHP code that displays your full name.

Exercise 2

Display your name and profession on two separate lines.

Exercise 3

Create variables for:

  • Your name
  • Your age
  • Your country

Display all three values.

Exercise 4

Create an HTML heading using PHP.

Exercise 5

Join these two strings:

PHP
Developer

The final result should be:

PHP Developer

Exercise 6

Create a variable called $course and display it inside an HTML <h2> element.

Exercise 7

Write a PHP comment that explains what your program does.

Exercise 8

Correct this code:

<?php

$name = "Ibrahim"
echo $Name;

Correct answer

<?php

$name = "Ibrahim";

echo $name;

The original code had two problems:

  • The variable assignment was missing a semicolon.
  • $Name used different capitalisation from $name.

Mini Project: Personal Introduction Page

Create a file named:

profile.php

Add this code:

<?php

$name = "Md Ibrahim Khalil";
$role = "PHP Full-Stack Developer";
$location = "Germany";
$learningGoal = "Build professional PHP web applications";

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title><?= $name ?> | Developer Profile</title>
</head>
<body>

    <main>

        <h1><?= $name ?></h1>

        <h2><?= $role ?></h2>

        <p>
            Location:
            <strong><?= $location ?></strong>
        </p>

        <p>
            Learning goal:
            <strong><?= $learningGoal ?></strong>
        </p>

    </main>

</body>
</html>

What this project teaches

This project helps you practise:

  • PHP opening tags
  • PHP variables
  • PHP inside HTML
  • Short echo syntax
  • HTML document structure
  • Dynamic page titles
  • Clean code formatting

Best Practices for PHP Beginners

Use the full opening tag:

<?php

Avoid short opening tags such as:

<?

Use meaningful variable names:

$userName

instead of:

$x

Write one statement per line.

Use consistent indentation.

Use comments only when they add useful information.

Use the .php file extension.

Run PHP files through a web server.

Prefer the short echo syntax inside templates:

<?= $value ?>

Omit the closing PHP tag in PHP-only files.

Can I run PHP without a server?

PHP requires a PHP interpreter.

For local development, you can use tools such as XAMPP, Laragon, MAMP, Docker, or PHP’s built-in development server.

Can PHP create complete websites?

Yes. PHP can handle:

1. User registration
2. Login systems
3. Forms
4. Database operations
5. APIs
6. Admin dashboards
7. E-commerce systems
8. Content management systems

Is PHP difficult for beginners?

PHP is considered beginner-friendly because its syntax is relatively simple and results can be seen quickly in a browser.

Should I learn HTML before PHP?

Basic HTML knowledge is highly recommended because PHP commonly generates and works with HTML.

Is PHP still useful?

PHP remains highly relevant for WordPress, Laravel, content management systems, e-commerce, APIs, and custom web applications.

Leave a Comment