Today, we’re going to discuss how you can run PHP files. If you're new to PHP programming, this article will help you learn how to run PHP scripts.
PHP is a server-side scripting language which is mostly used to build web-based applications. These may range from a very simple blog to a full-fledged eCommerce website. In fact, PHP is one of the most popular server-side scripting languages for web development.
If you’re a beginner in PHP programming and you don’t know what a PHP file is, I would recommend that you review the basics of a PHP file.
In this post, we’ll discuss different ways to run PHP files.
Different Ways to Run PHP Files
There are two ways to run PHP files. The preferred way of running PHP files is within a web server like Apache, Nginx, or IIS—this allows you to run PHP scripts from your browser. That’s how all PHP websites work! The other way is to run PHP scripts on the command line, and it doesn't require you to set up a web server.
Of course, if you want to publish your PHP pages online, you’ll have to go with the web server setup. On the other hand, running PHP scripts from the command line is useful for performing routine tasks. These are generally configured to run in the background as jobs and are run by the
php command without a web server.
In fact, many PHP scripts or applications nowadays come with built-in commands that are used to perform various operations like installing software, exporting and importing database entities, clearing the cache, and more. You may have heard of Composer—it's a dependency manager for PHP and is one of the most popular tools built for command-line PHP.
Run a PHP File on a Web Server
If you want to run PHP scripts from a web server, you need to configure it with one of the web servers that supports it. For Windows, the IIS web server is one of the most popular. On the other hand, Apache and Nginx are widely used web servers for other operating systems.
The good news is that most hosting providers will have a web server with PHP already set up for you when you log in to your new server.
Run a PHP File in the Browser for Development With XAMPP
If you want to run a PHP file in the browser on your own computer, you'll need to set up a PHP development stack. You’ll need at least PHP, MySQL, and a server like Apache or Nginx. MySQL is used to set up databases your PHP applications can work with. Of course, you can choose to work with other database engines, but MySQL is one of the most popular databases used with PHP.
Instead of downloading all this software separately and configuring it to work together, I recommend you just download and install a program like XAMPP. It will include all the necessary software and will get you set up to run PHP in no time. And yes, it supports Windows, Linux, and macOS.
XAMPP contains everything you need to build your web pages locally. It's hassle-free and allows you to start PHP development right away.
For this tutorial, I'll use the XAMPP software to demonstrate PHP examples. So download and install it if you want to follow along and run the PHP examples. If you face any problems during installation, feel free to post your queries using the feed at the end of this article.
Once you've installed the XAMPP software locally and have it running successfully, you should be able to see the default XAMPP page at http://localhost in your browser.
Run Your PHP File in XAMPP
The first thing you'll need to know after installing XAMPP is the location where you will put your PHP files. When you install the XAMPP software, it creates the htdocs directory, which is the document root of your default web server domain: localhost. So if you go to http://localhost/example.php, the server will try to find the example.php file under the htdocs directory.
Depending on the OS you’re using, the location of the htdocs directory varies. For Windows, it would be located at C:\xampp\htdocs. On the other hand, it would be located at /opt/lampp/htdocs for Linux users. Once you’ve found the location of the htdocs directory, you can start creating PHP files right away and running them in your browser!
phpinfo() is a very useful function that gives you information about your server and PHP setup. Let’s create a phpinfo.php file under the htdocs directory with the following contents:
<?php phpinfo(); ?>
Now, go ahead and run it in your browser at http://localhost/phpinfo.php, and you should see output like this:
If you haven’t realized yet, let me tell you that you have just run your first PHP file on the web server! It may be a small one, but it's a significant step towards learning PHP website development.
In fact, you could also create new directories under the htdocs directory to organize your PHP scripts. For example, you could create the datetime directory under htdocs for examples related to date and time. So if you create a today_date.php file under the htdocs/datetime directory, you could access it in the browser by going to http://localhost/datetime/today_date.php.
So in this way, you could create and run PHP scripts with a web server. In fact, this is what you are going to use most of the time in your day-to-day PHP development.
In the next section, we’ll see how you could run PHP scripts using the command line.
How to Run a PHP File Using the Command Line
When it comes to running PHP scripts on the command line, you should be aware of the location of the PHP executable file in your PHP installation.
For Windows users, you should be able to find the php.exe file under the directory where you have installed your PHP. On the other hand, if you're a Linux or macOS user, you should be able to find it at /usr/bin/php. In fact, on Linux or macOS, you can just use the
php shortcut from any directory. Once you know the location of your PHP executable file, you just need to provide the name of the PHP file which you want to execute from the command-line interface.
As a Windows user, though, you'll need to type the full path to the PHP executable to run a PHP script. The PHP executable is usually available at C:\php7\php.exe, so you can use it to execute the PHP file as shown in the following command.
For Linux, you don't need to type the whole path to the PHP executable.
As we discussed earlier, command-line PHP scripts are generally used for routine application-specific tasks, like:
- clearing or generating application caches
- exporting/importing system entities
- batch processing
- other general-purpose tasks
Generally, these kinds of tasks take a long time to execute and are not suited to running in a web environment since they cause timeout errors.
It’s important to note that when you’re running PHP scripts on the command line, you won’t have access to any
$_SERVER variables, since these are only initiated if you’re running PHP on a web server. Thus, it’s not recommended to rely on these variables if you’re planning to run your scripts using the command line.
As a PHP programmer, it is important to understand how the command-line PHP interface works. In fact, a lot of PHP software and frameworks nowadays come with a built-in command-line tool which allows you to perform a wide range of utility tasks from the CLI itself.
Today, we discussed how you could run PHP files on your system. Specifically, we went into the details of how to execute PHP scripts in a browser along with a web server and how to run it with a command-line interface as well.
As a beginner, it’s important for you to understand the basic concepts in the first place. And I hope this article has helped you to move a step further in your PHP leaning. Don’t hesitate to ask if you have any queries by using the feed below.
The Best PHP Scripts on CodeCanyon
Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. With a low-cost, one-time payment, you can purchase one of these high-quality WordPress themes and improve your website experience for you and your visitors.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
- PHP11 Best PHP Event Calendar and Booking Scripts... and 3 Free OptionsMonty Shokeen
- PHP10 Best PHP URL Shortener ScriptsMonty Shokeen
- PHP18 Best Contact Form PHP Scripts for 2020Monty Shokeen
- PHPComparing the 5 Best PHP Form Builders (And 4 Free Scripts)Monty Shokeen
- PHPCreate Beautiful Forms With PHP Form BuilderAshraff Hathibelagal
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post