How to Run a File in PHP
This tutorial demonstrates the ways of running a file in PHP. There are two ways to run a PHP file:
- We can run a PHP file using a web server, which can be Apache, Nginx, or IIS. This method may allow you to run PHP scripts on a web browser.
- We can also run a PHP file by a command line without any server.
Run a PHP File in XAMPP
The Xampp is part of the Apache server, which helps us run PHP scripts locally on the computer. If you have to run PHP scripts from a web server, you would need to configure it with one of the internet servers that supports it.
For Windows, the IIS web server is considered as one of the commonly used servers. Moving forward, Apache and Nginx are broadly utilized web servers for other working frameworks.
Running a PHP file requires specific tools like PHP and MySQL and servers like Apache. Instead of downloading all of the ones we mentioned, we recommend you only download and install a program like XAMPP - this will provide you with everything you need in the installer package. The best thing about XAMPP is that it supports Windows, Linux, and macOS.
Follow the steps below to run a PHP file through XAMPP.
-
The first step is to download the XAMPP server.
-
After the download, go to the web browser and check your server to check if it’s working successfully. Write
http://localhost
on the browser, and look at the XAMPP default page. -
After that, the most important thing is the location where you put the PHP file. Upon installation of the XAMPP computer program, you’ll find the
htdocs
catalog, which is the archive root of your default web server space:localhost
. So when you go to the http://localhost/example.php link, the server will attempt to discover theexample.php
record beneath thehtdocs
directory. -
Depending on the OS you’re utilizing, the area of the
htdocs
directory changes. For Windows, it would be found atC:\xampp\htdocs
. For Linux and macOS users, it would be found at/opt/lampp/htdocs
. Once you’ve found the area of thehtdocs
directory, you’ll begin making the PHP script right away and running them in your browser.
With the steps above, you can easily run a PHP file on your local browser and easily work on PHP.
Run PHP Files Using the Command Line
For running the PHP file by using the command line, you must know about the location of the file. Your IDE terminal should be in that same location, and the executable script should be in the PHP installation.
For Windows clients, you need to discover the php.exe
file beneath the directory where PHP is introduced. On the other hand, Linux or macOS users have to find it at /usr/bin/php
.
On Linux and macOS, you can fairly utilize the PHP shortcut from any directory. Once you know the area of your PHP executable record, you have to enter the title of the PHP record, which you need to execute from the command line interface.
For Linux and macOS users, use this command below.
php my_example.php
If you use Windows, you should know the whole location of the executable script for running. The PHP executable is accessible at C:\php7\php.exe
; you’ll utilize it to execute the PHP file after giving the command.
For Windows users, use this command below.
C:\php7\php.exe my_example.php
As we examined before, the command-line PHP scripts are primarily utilized for scheduling application-specific assignments, like the following.
- clearing or creating application caches
- exporting/importing framework entities
- batch processing
- other general-purpose tasks
Generally, these sorts of tasks take a long time to execute and are not suited for web environment executions since they cause timeout errors.