how to run php
How to Run PHP Files
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.
You Are at | : | Basics > How to Run PHP Files |
Previous Article | : | Working with XAMPP |
Next Article | : | Getting Information about PHP Installation |
Share with Your Peers.
We love to hear what you think about this article. Please provide your opinion, suggestions and improvements using following form. Note that submitted feedback is not displayed but we will get back to you if it needs a reply.
How to Run a PHP File
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:
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:
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.
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.
Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
Running PHP script from the command line
How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?
3 Answers 3
To run php interactively:
(So you can paste/write code in the console.)
To make it parse a file and output to the console:
Parse a file and output to another file:
Do you need something else?
To run only a small part, one line or like, you can use:
If you are running Linux then do man php at the console.
If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:
Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.
On SuSE, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an «apache2» directory and a «cli» directory. Each has a «php.ini» file. The files are for the same purpose (php configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.
If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the gearman module, while the cli php.ini isn’t. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.
You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.
Or, you could find the line that loads the gearman module in the Apache file and copy/paste just it to the CLI file.
How can I run a PHP script inside a HTML file?
How can I run simple PHP code inside a .html file?
8 Answers 8
To execute ‘php’ code inside ‘html’ or ‘htm’, for ‘apache version 2.4.23’
Go to ‘/etc/apache2/mods-enabled’ edit ‘@mime.conf’
Go to end of file and add the following line:
BEFORE tag ‘ ‘ verified and tested with ‘apache 2.4.23’ and ‘php 5.6.17-1’ under ‘debian’
thanks for the ideas but none works here. So i did that. I am using xampp last version on 2014. go to \xampp\apache\conf\extra\httpd-xampp.conf.
we will find this bit of code:
Focus on second line, so we must to change to:
And that is it. Works good!
1- Excute php page as external page.
2- write your html code inside the php page itself.
3- use iframe to include the php within the html page.
Here, the JQuery is loaded and as soon as the pages load, the ajax call a php file from where the data is taken, the data is then put in the div
Yes, you can run PHP in an HTML page.
I have successfully executed PHP code in my HTML files for many years. (For the curious, this is because I have over 8,000 static HTML files created by me and others over the last 20 years and I didn’t want to lose search engine ranking by changing them and, more importantly, I have too many other things to work on).
That solution appears to be deprecated now, though it might work for you.
Here’s what’s working for me now:
Open a text editor such as wordpad, notepad, nano, etc. and add the following line:
If you want to use PHP 5.4 instead of PHP 5.2 then use the following line instead:
You can add additional lines for other file extensions if needed.
How to Run a PHP Script? Step By Step Guide!
Hello and welcome to the start of codeofaninja.com’s series of web development articles!
Overview
Setting up a development environment for PHP programming is easy. Download the code editor you prefer, I personally like atom.io text editor.
Next is to install XAMPP, the most popular PHP development environment. This package contains Apache, PHP & MariaDB or MySQL database applications.
Many people emailed me with a main question: Mike, how to run a PHP script? This post is my answer to you guys and to those people who will need this in the future.
In the following tutorial, we will learn how to install XAMPP, how to run a PHP script, manage database with PhpMyAdmin and run a sample PHP script that fetches a record from the database.
Install XAMPP
Go to this link and download XAMPP for your operating system. XAMPP is available for Windows, Linux or Mac.
Here’s a video about how you can install and use XAMPP.
Run Your First PHP Script
The following is an example about how to run a PHP script. What this program does is show a «Hello World!» text on the screen or webpage.
Go to XAMPP server directory
I’m using Windows, so my root server directory is «C:\xampp\htdocs\».
Create hello.php
Create a file and name it » hello.php «
Code Inside hello.php
Open hello.php and put the following code.
Open New Tab
Run it by opening a new tab in your browser
Load hello.php
On you browser window, type http://localhost/hello.php
Output
You should see the following output.
Great job, you just run a PHP script!
Manage MySQL with PhpMyAdmin
MySQL is an open-source relational database management system (RDBMS). MySQL is a popular choice of database for use in web applications.
phpMyAdmin is a free and open source tool written in PHP intended to handle the administration of MySQL with the use of a web browser. In the following examples, we will see how easy we can handle MySQL with PhpMyAdmin.
Create a Database
Create a Table
Insert Data
Click the «products» table.
Click the «Insert» tab.
Fill out the form, mimic the data on the following image. Click the «Go» button.
Great job! We now have a database, a table inside the database and a record inside the table.
Useful Videos
1. Create a database and import MySQL file.
2. Create a database and create table.
Run PHP Script with Database
In the following steps, we will run a PHP script that fetches one record from the MySQL database.
Go to XAMPP server directory
Go to your «C:\xampp\htdocs\» directory
Create read_one.php
Create a file and name it » read_one.php «
Code Inside read_one.php
The numbers 1-8 in the following code are called «code comments». It explains each part of our simple code below. Open read_one.php and put the following code.
Open Your Browser
Run it by opening you your browser
Load read_one.php
On you browser window, type http://localhost/read_one.php
Output
You should see the following output.
Awesome! You are now ready to learn more about web programming and development.
Online Resources
Here in codeofaninja.com, we want to simplify learning for you to actually build something. But it is also important for you to read and study more. The following are my suggestions where to learn more.
You can always go back to the list above while you go along our series of web programming tutorials.
What’s Next?
Related Tutorials
Some Notes
Found An Issue?
Before you write a comment, remember to read this guide and our code of conduct.
Subscribe to CodeOfaNinja
We constantly improve CodeOfaNinja. We update our tutorials and source codes. Receive valuable web programming tutorials and updates to your email. Subscribe now!