Writing Your First PHP Program
Writing Your First PHP Program
In this lesson, you will write your first PHP program and understand how PHP code executes on a server. This is the first step toward building dynamic web applications using PHP.
Creating Your First PHP File
To begin, make sure your local server using XAMPP is running. Then follow these steps.
Step 1: Open htdocs Folder
Go to the XAMPP installation directory and open the htdocs folder. This is where all your project files will be stored.
Step 2: Create a PHP File
Create a new file and name it first.php.
Step 3: Add PHP Code
Write the following code inside the file:
echo “Hello, World!”;
?>
Understanding the Code
PHP Tags
PHP code always starts with <?php and ends with ?>. These tags tell the server that the code inside them should be executed as PHP.
echo Statement
The echo statement is used to display output on the screen. In this case, it prints “Hello, World!”.
Running Your PHP Program
Open your browser and type the following URL:
http://localhost/first.php
If everything is set up correctly, you will see the output displayed in your browser.
Mixing PHP with HTML
One of the powerful features of PHP is that it can be embedded within HTML.
Example:
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1><?php echo “Welcome to PHP”; ?></h1>
</body>
</html>
This allows you to create dynamic content inside web pages.
Common Beginner Mistakes
Forgetting PHP Tags
If you do not use <?php ?>, the server will not execute the code.
Saving File with Wrong Extension
Make sure the file is saved with .php and not .html.
Not Running Server
Always ensure Apache is running in XAMPP before executing PHP files.
Why This Lesson is Important
Writing your first PHP program builds confidence and gives you a foundation for advanced topics like forms, databases, and authentication systems.
Start Your Learning Journey
Want to explore more courses like this? click here for free courses
FAQs – Writing Your First PHP Program
What is the first PHP program
The most common first PHP program is printing “Hello, World!” using the echo statement.
How do I run a PHP file
You need a local server like XAMPP and access the file through localhost in a browser.
Can PHP run without a server
No, PHP requires a server environment to execute.
What does echo do in PHP
The echo statement outputs text or variables to the browser.
Can I use PHP inside HTML
Yes, PHP can be embedded inside HTML to create dynamic web pages.



