If you’re new to PHP programming, you may be wondering how you can run a file. After all, writing code is only part of the battle with programming—you need to know how to execute your code. With an HTML file, all you need to do is double-click the file and it’ll open up in your default browser. Unfortunately, you cannot do the same with PHP files. It takes a little more work, but with a little patience, you can run your PHP files in no time. Read on to learn how you can run a PHP file!
To understand why you can’t simply open a PHP file like you can an HTML file to run it, you need to understand the language. PHP is a server-side language which means it does not interact directly with a web browser. Rather, your web browser must communicate with your server-side program for your PHP code to execute. For the browser to communicate with your PHP code, you must follow one of the two following methods
Running PHP Files
To get your PHP code to do something—hopefully what you want it to do—you have to use a web server or a command line. A web server is either hardware or software that communicates with requests from a browser and renders the necessary return that the browser expects. Popular web servers include Apache, Nginx, and IIS. If your goal is to create an application to publish on the internet, you’ll need to use a web server.
Your other option to run a PHP file is through the use of a command-line interface. This process is easier as you don’t have to set up a web server. However, this option is primarily for testing programs, performing basic tasks, and similar functions.
How to Run a File with a Web Server
If you’re creating a website or application from scratch, as in you’re not using a hosting provider like WordPress, you’ll need to set up your environment. Most likely, your application will need an array of tools with the most important being a database and a web server.
To make things easier, you can go the route of downloading the popular web server stack called XAMPP. This product provides everything you need to get your PHP project up and running. Plus, it will run on any computer or laptop you have. Thus, to easily run a PHP file, you should download XAMPP to get started.
Once you’ve installed XAMPP, and you have it running, you need to locate the folder where your PHP files will be stored. Depending on your operating system—Windows, Linux, macOS—your folder will be located in different places. Regardless, through the installation of the software, you’ll have a folder called htdocs. Here, you can create or move your first PHP file.
As XAMPP should still be running at this point, all you need to do is go to the browser and type in the correct domain name. The domain name will always begin with http://localhost/XXXX.php where XXXX is the name of your file.
While this first file seems quite simple, as you begin to build a project, you’ll need to learn how to organize your files and folders. That means, within your htdocs folder, you’ll have several subfolders, and within those, more folders.
How to Run a PHP File with the Command Line
Many developers and even computer-savvy people use the command line for daily tasks such as opening files, creating new files, and deleting files. When you know how to navigate a command line, the speed at which you can accomplish tasks is attractive. The same is true with running PHP files via the command line.
Before you can run a PHP file through the command line, PHP must be downloaded on your computer. You can go straight to the PHP website.
Assuming you’ve already done this, to run the file, you first need to find where your files are through the command line. If you’re a Windows user, your PHP files will likely reside in the same location as your PHP installation. If you’re a Linux or a macOS user, you should be able to find your files at the user root. It may look like usr/bin/php.
Once you’re in the right location, follow these steps.
- Navigate to the root folder where your PHP files are held
– This is likely where your installation folder resides
- To run the file, type the following command and press the enter key Php file-name.php
And that’s it. You now know how to run a PHP file through a command-line interface and the help of a web server. Once you get the hang of running these files, you can begin to build on your programming skills.