Colleague HQ India

Featured

PHP Tutorial - 2

P PHP

PHP is one of the most popular technologies for creating dynamic websites and web applications. In this beginner-friendly tutorial, you will learn how to create and run your very first PHP script.

Before getting started, make sure PHP is already installed on your system. You can download it from the official PHP website and follow the installation instructions for your operating system.

Step 1: Create Your First PHP File

Create a new file named:

hello.php

Now add the following code inside the file:

<?php

echo "Hello World!";

?>

This simple script uses PHP’s echo statement to display text in the browser.

Step 2: Start the PHP Development Server

Open your terminal or command prompt and navigate to the folder where your hello.php file is stored.

Run the following command:

php -S localhost:8000

This command starts PHP’s built-in development server.

Step 3: Open the File in Your Browser

After starting the server, open your browser and visit:

http://localhost:8000/hello.php

If everything is configured correctly, you will see:

Hello World!

displayed on the screen.


Embedding PHP Inside HTML

One of PHP’s greatest advantages is that it can be embedded directly into standard HTML pages.

Example:


Output:


This example demonstrates how PHP works seamlessly inside HTML documents. The server processes the PHP code and sends the final HTML output to the browser.


Understanding PHP Tags

PHP code is written between special opening and closing tags:

<?php

// PHP code here

?>

These tags allow developers to switch between HTML and PHP whenever needed inside the same file.


Why PHP Files Work

A PHP file behaves like a normal HTML file, but the .php extension tells the web server to process the file using PHP before sending the output to the browser.

This means visitors only see the final result — not the actual PHP code running behind the scenes.


Important Notes for Beginners

Line Feeds and Formatting

HTML ignores most line breaks, but writing clean and properly formatted code is still considered good practice. PHP also automatically removes line breaks immediately after a closing ?> tag in some cases.

Choosing a Text Editor

You can write PHP code using:

  • Visual Studio Code

  • Sublime Text

  • PHPStorm

  • Notepad++

  • Vim

Using an editor with syntax highlighting makes coding much easier and more readable.

Avoid Word Processors

Applications like Microsoft Word or similar word processors are not designed for coding because they add formatting that can break PHP files. Always save PHP scripts as plain text.


Getting System Information with PHP

PHP provides a built-in function called phpinfo() that displays useful information about your PHP installation and server configuration.

Create a file with the following code:

<?php

phpinfo();

?>

When executed, this script displays:

  • PHP version

  • Installed extensions

  • Server information

  • Configuration settings

  • Environment variables

  • Loaded modules

This is one of the most useful tools for checking whether PHP is installed and configured correctly.


Congratulations! You have now created your first PHP-enabled page and learned the basics of how PHP works with HTML and web servers.