Open In App

How to run PHP programs ?

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

PHP (Hypertext Preprocessor) is a popular server-side scripting language primarily used for web development. It is designed to generate dynamic web pages and interact with databases. Running PHP programs involves setting up a development environment, whether locally or on a live server.

In this article, we’ll discuss how to run PHP programs on your local machine and on a web server.

Setting Up the Environment to Run PHP

Step 1: Download the XAMPP

Go to the official website of XAMPP and install itXAMPP.

Step 2: Start the XAMPP Program Control Panel.

Click on the “Start”  button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

Screenshot-2025-04-14-145422

XAMPP

Step 3: Create the PHP File

  • Place your PHP files in the “htdocs” folder located under the “XAMPP” folder on your drive.
  • The initial path is “your_drive_letter:\xampp\htdocs” for your Web server.
  • Make sure that your PHP files are saved as a “.php” file extension.

Now, create the file index.php inside the htdocs folder and write your below given PHP code in it.

PHP
<?php
echo "Hello, World!";
?>

Open your terminal and write the below code in the terminal to run the PHP program.

php index.php

Output:

Screenshot-2025-04-14-150929

Hello World PHP

Running PHP on Local Web Server

For running the PHP on local web sever follow the below given command:

  • Open XAMPP and start the Apache server.
  • Write the below code in your index.php file.
PHP
<?php
	echo "Hello geeksforgeeks";
?>

In your browser, go to:

http://localhost/index.php

This will run your PHP script through the local server and display the output in the browser.

Output

Screenshot-2025-04-14-153525

How to run PHP programs ?

Conclusion

To run PHP programs, you need to set up a suitable environment, either locally or on a live web server. For local development, you can install server packages like XAMPP, WAMP, or MAMP, which provide PHP, Apache, and MySQL in one installation. Once set up, you can create PHP files with a .php extension and place them in the server’s root directory (e.g., htdocs for XAMPP). After starting the server, you can access the PHP file in your browser using http://localhost/filename.php.



Next Article

Similar Reads