How do you execute php in web?

If you double click on a HTML file (files with .html or .htm extension), it would open on your web browser. But same won’t happen if you double clicked on a PHP file (probably it would open in an editor). The reason is PHP files first need be processed in a web server before sending their output to the web browser.

Therefore before running PHP files, they should be placed inside the web folder of a web server and then make a request to desired PHP file by typing its URL in the web browser. If you installed a web server in your computer, usually the root of its web folder can be accessed by typing http://localhost in the web browser. So, if you placed a file called hello.php inside its web folder, you can run that file by calling http://localhost/hello.php.

Web folder can be changed based on your web host (if you hosted your web site online) or the method you installed the web server in your computer. If you used XAMPP to install Apache (web server) in your computer then the web folder would be htdocs which is under the root directory of XAMPP.

Where to Head from Here...

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:

Now, go ahead and run it in your browser at http://localhost/phpinfo.php, and you should see output like this:

How do you execute php in web?
How do you execute php in web?
How do you execute php in web?

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.

C:\php7\php.exe my_example.php

For Linux, you don't need to type the whole path to the PHP executable.

php my_example.php

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. 

Conclusion

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.

How do you execute php in web?
How do you execute php in web?
How do you execute php in web?

Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.

Did you find this post useful?

How do you execute php in web?

Software Engineer, FSPL, India

I'm a software engineer by profession, and I've done my engineering in computer science. It's been around 14 years I've been working in the field of website development and open-source technologies. Primarily, I work on PHP and MySQL-based projects and frameworks. Among them, I've worked on web frameworks like CodeIgnitor, Symfony, and Laravel. Apart from that, I've also had the chance to work on different CMS systems like Joomla, Drupal, and WordPress, and e-commerce systems like Magento, OpenCart, WooCommerce, and Drupal Commerce. I also like to attend community tech conferences, and as a part of that, I attended the 2016 Joomla World Conference held in Bangalore (India) and 2018 DrupalCon which was held in Mumbai (India). Apart from this, I like to travel, explore new places, and listen to music!

How do I run a PHP web application?

To run a PHP Web Page:.
Click the arrow next to the Run button. on the toolbar and select Run Configurations -or- go to Run | Run Configurations. A Run dialog will open..
Double-click the PHP Web Page option to create a new run configuration..

How PHP is executed?

There are three different ways of supplying the CLI SAPI with PHP code to be executed:.
Tell PHP to execute a certain file. $ php my_script.php $ php -f my_script.php. ... .
Pass the PHP code to execute directly on the command line. ... .
Provide the PHP code to execute via standard input ( stdin )..

Is PHP executed on Web browser?

PHP Is Not Part of Your Browser. And here's where things change from the easy, browser-centric view of the world. When you download a web browser, you get HTML, CSS, and JavaScript, but you do not get PHP. PHP scripts—which you'll soon be writing—have to be interpreted by the PHP interpreter program, called php.

How do I open PHP in browser?

If you installed a web server in your computer, usually the root of its web folder can be accessed by typing http://localhost in the web browser. So, if you placed a file called hello. php inside its web folder, you can run that file by calling http://localhost/hello.php.