Writing First PHP Program

Introduction

Writing your first PHP program is the starting point of learning server side web development. PHP allows you to create dynamic web pages that run on the server and display output in the browser. A simple first program helps you understand PHP syntax, structure, and execution flow.

Understanding PHP File Structure

A PHP file is saved with the extension .php. It can contain both HTML and PHP code. PHP code is written inside special tags that tell the server to process it.

Basic PHP Syntax

PHP code always starts with PHP opening tag and ends with closing tag. Inside these tags, you write instructions that the server executes.

Writing Your First PHP Program

To create your first program, follow these steps:

Create a new file and save it as index.php

Write the following code:

php <?php echo Hello World ?>

Run the file on a local server such as XAMPP or WAMP. Open your browser and go to localhost to see the output.

Expected Output

Hello World

How PHP Code Works

When you open a PHP file in the browser, the server processes the PHP code first. It converts it into plain HTML and sends the result to the browser. The browser only shows output, not the PHP code.

Key Points for Beginners

PHP code must be written inside PHP tags
Every statement ends with a semicolon
echo is used to display output on the screen
PHP runs on the server, not in the browser

Common Mistakes to Avoid

Forgetting semicolons at the end of statements
Not saving file with .php extension
Running file without local server setup
Typing incorrect syntax in echo statement

Practical Exercise

Create a PHP file and display your name using echo statement
Modify the program to show your city and country
Run the file in browser using localhost server

Career Importance

Learning how to write your first PHP program builds the foundation for backend development. It helps you understand how dynamic websites work and prepares you for advanced PHP topics like forms, databases, and frameworks.

Home Ā» PHP Fundamentals (Beginner Level) > Introduction to PHP > Writing First PHP Program