Introduction
In the previous tutorial, you learned what backend development is and how a server processes requests. Now it is time to start writing actual backend code.
For this backend development series, we will use PHP as our main programming language. PHP is widely used for building dynamic websites, web applications, login systems, content management systems, and database-driven websites.
Before working with databases or creating complete applications, you need to understand the basic PHP syntax.
In this tutorial, you will learn how to create a PHP file, write your first PHP program, display output, use comments, and understand the basic structure of PHP code.
What Is PHP?
PHP is a server-side scripting language mainly used for web development.
Unlike HTML, PHP code is processed on the server before the result is sent to the browser.
A PHP file normally uses the .php extension.
For example:
index.php
login.php
register.php
profile.php
A PHP file can contain HTML and PHP code together.
Creating Your First PHP File
Create a new file named:
index.php
Inside the file, write:
<?php
echo "Hello, World!";
?>
Save the file and run it through a PHP-enabled web server.
The browser will display:
Hello, World!
This is your first PHP program.
Understanding PHP Tags
PHP code normally starts with:
<?php
This tells the server that PHP code begins here.
For example:
<?php
echo "Welcome to PHP";
?>
The ?> closing tag can be used to end PHP code.
However, when a file contains only PHP code, the closing tag is usually omitted:
<?php
echo "Welcome to PHP";
This is a common and recommended practice because it helps prevent accidental whitespace or output after the PHP code.
Displaying Output with echo
The echo statement is one of the simplest ways to display information in PHP.
Example:
<?php
echo "Hello, World!";
You can also display HTML:
<?php
echo "<h1>Welcome to IncomeVA</h1>";
The browser will render the heading normally.
You can also write:
<?php
echo "<p>Learn web development step by step.</p>";
This allows PHP to generate HTML dynamically.
Using Variables
Variables are used to store information.
In PHP, a variable starts with the $ symbol.
Example:
<?php
$name = "John";
echo $name;
The output will be:
John
You can also store numbers:
<?php
$age = 25;
echo $age;
Output:
25
A variable can store different types of information, such as text, numbers, and more.
Using Multiple Variables
You can create multiple variables in the same PHP program.
<?php
$name = "John";
$age = 25;
echo $name;
echo $age;
You can also combine them with text:
<?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.
This is one of the basic ways PHP can generate dynamic content.
PHP Statements and Semicolons
Most PHP statements end with a semicolon ;.
For example:
<?php
$name = "John";
$age = 25;
echo $name;
echo $age;
The semicolon tells PHP that the statement has ended.
Forgetting the semicolon can cause a PHP syntax error.
For example:
<?php
$name = "John"
echo $name;
This code is incorrect because the variable assignment is missing a semicolon.
Correct version:
<?php
$name = "John";
echo $name;
Adding Comments
Comments are notes written inside your code. PHP does not execute them.
A single-line comment can be written using //:
<?php
// This is a comment
echo "Hello";
You can also use #:
<?php
# This is also a comment
echo "Hello";
For multiple lines, use:
<?php
/*
This is a
multi-line comment.
*/
echo "Hello";
Comments are useful for explaining code and making larger projects easier to understand.
PHP and HTML Together
One of the useful features of PHP is that you can combine PHP with HTML.
Example:
<!DOCTYPE html>
<html>
<head>
<title>My PHP Page</title>
</head>
<body>
<h1>Welcome</h1>
<?php
echo "<p>This paragraph was generated by PHP.</p>";
?>
</body>
</html>
The server processes the PHP code and sends the resulting HTML to the browser.
This combination is commonly used when building dynamic websites with PHP.
A Simple Practical Example
Let's create a small profile page using PHP variables.
<?php
$name = "Alex";
$age = 22;
$country = "Bangladesh";
?>
<!DOCTYPE html>
<html>
<head>
<title>My Profile</title>
</head>
<body>
<h1><?php echo $name; ?></h1>
<p>Age: <?php echo $age; ?></p>
<p>Country: <?php echo $country; ?></p>
</body>
</html>
Here, PHP stores the information in variables and HTML displays the information on the page.
This is the basic idea behind dynamic web pages.
Common Beginner Mistakes
When starting PHP, beginners often make a few simple mistakes:
1. Forgetting $
Incorrect:
name = "John";
Correct:
$name = "John";
2. Forgetting the semicolon
Incorrect:
$name = "John"
Correct:
$name = "John";
3. Using PHP without a PHP server
A PHP file generally needs to be processed by a PHP-enabled server. Simply opening a .php file directly in a browser will not execute PHP code correctly.
Practice Task
Create a file named:
profile.php
Create three variables:
$name
$age
$profession
Then display them on an HTML page.
For example, your page should show:
My name is Alex.
I am 22 years old.
I am a Web Developer.
Try changing the values and observe how the output changes.
Conclusion
In this tutorial, you learned the basic structure of PHP and created your first PHP program. You also learned how to use echo, variables, comments, semicolons, and PHP with HTML.
These concepts are the foundation of PHP development.
In the next tutorial, we will learn about PHP Variables, Constants, and Data Types in more detail.
Next Tutorial: PHP Variables, Constants, and Data Types