Does php run in browser?

How do I run a PHP file in my browser? I know what the file is for and I know how it looks because I tested it online. But now I downloaded it locally to my computer and I do not know how to run it on my browser. Is there a way maybe to link it to a HTML like you do with JavaScript or CSS to make it work on a web browser also after it is online how will I manage it to work?

If I can not link then maybe I can use iframe to see it in a HTML iframe. Is that possible? Yet the question is not based about iframe, but I do not exclude the iframe possibility too. That is why I ask of it too.

Or even better, if there is a way to both link and use iframe for the PHP?

The entire PHP file I got is a email function that works and looks a loot like a HTML and JavaScript file, but it is written in PHP. If I simply change the name to the file from PHP to HTML I can open it in the browser and it looks like a HTML file with few defects since it is a PHP file after all.

The file I downloaded was from here: https://www.hscripts.com/scripts/php/registrationMailer.php

And it is this PHP file I am specifically intrested in: https://www.hscripts.com/scripts/php/HMAD/hmailer.php

NOTE: I am not asking of "PHP code is not being executed, instead code shows on the page" as the other page is asking. I am asking of "How do I show / run a PHP file in a browser? As if it was a webpage."

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...

Admittedly this post is less about the actual task of programming, and more about the behind-the-scenes workings of PHP's execution environment.

Introduction

PHP is a popular server-side language that is fantastic for web applications. Some of the largest companies and organizations from around the world utilize PHP for their operations (some of them you probably use every single day). A large amount of web sites and applications are powered by PHP; therefore an understanding of the PHP language is mandatory to fully understand and accept the power behind popular frameworks (such as Laravel, CodeIgniter or Symfony), and how popular websites and applications may be handling user's data.

To kick start this mini-series of posts, we will begin by looking at how users interact with the web server, and how the web server and PHP talk to one another.

The Language

PHP is an interpreted language. This means that you will write code statements (lines of code) and when a page is requested, the PHP interpreter will load your PHP code, parse it and then execute it. This differs from other languages, such as Java or C#, where the source code is compiled and then executed. This is useful for web development in the fact that you do not have to re-compile your source code for trivial code changes and the changes have immediate effect on all subsequent requests.

PHP is written as standard text files with the .php extension. PHP files are often saved within a folder in a web server's public directory (or a web root directory). On most systems this will either be named public or public_html. For example, if a file was saved as index.php in a web root directory, a user could access it by typing http://www.example.org or http://www.example.org/index.php.

1public/

2| index.php

PHP and Default Pages (Directory Indexes)

Most PHP and web server setups let you use a file named index.php just as you would use an index.html file. However, make sure you know which one takes priority so you don't get unexpected results (usually index.php).

The Request Life-cycle

So what exactly is happening when a user types in the URL http://example.org? When a user types in http://example.org in a Web client (a browser, for instance), the client issues a GET request to the server (let's assume that we are both using Apache). When Apache gets this request, it looks for a file named index.php (or index.html, remember the directory indexes from earlier?). If a file named index.php is found, Apache essentially says "Hey, this is a PHP file because it has the .php extension. I am going to give this to the PHP interpreter".

After Apache decides that is is a PHP file, it gives it to the PHP interpreter. When PHP receives the file it reads through it and executes any PHP code it can find. After it is done with the file, the PHP interpreter gives the output of the code, if any, back to Apache. When Apache gets the output back from PHP, it sends that output back to a browser which renders it to the screen.

PHP And Apache Output

Beginners to PHP programming might often ask questions like "How do I make an image with PHP?" or "How do I make a text box with PHP?". In all honesty, PHP does neither. In a traditional sense, the main goal of PHP is to generate some HTML document that a browser can render.

However, modern applications built with client-side MVC frameworks often see the role of PHP change to just interacting with server-side data storage.

Let's take another look at this process with a diagram. In this diagram, we will assume the user is going to the Laravel website at http://laravel.com/. The following figure has circled numbers that will highlight the various stages of the request. A step-by-step explanation of each step follows the figure.

Does php run in browser?

Step 1 The user enters `http://laravel.com` into their browser and taps/hits 'enter'. Step 2 After the user has tapped/hit 'enter', the browser sends the page request over the Internet to the web server. Step 3 The web server gets the request and analyzes the request information. Apache realizes that we didn't specify a file, so it looks for a directory index and finds `index.php`. Step 4 Since Apache knows to send files that end with the `.php` file extension to the PHP interpreter, it asks PHP to execute the file. Step 5 In this step, PHP is executing the code contained in the `index.php` file from the request. During this step, PHP may interact with databases, the file system or make external API calls, amongst other things. Step 6 After PHP has finished executing the `index.php` file, it sends the output back to Apache. Step 7 Apache receives the output from PHP and sends it back over the Internet to a user's web browser. This is called the `web response`. Step 8 The user's web browser receives the response from the server, and renders the web page on a computer or device.

As you can see, PHP interacts with a web server in a very real way. The actual request process is very simple, and one of the reasons that PHP is so well suited for web application development.

Conclusion

This concludes this first article in a little series about the basics of programming with PHP. The next articles will cover things such as variables, operators and functions. After that, we will take a dive into the world of Object Oriented Programing (OOP) and see how that fits in with PHP.

Thanks for taking the time to read this post! If you found this article useful and want to help support more work like this, please consider sponsoring my work on GitHub, or by checking out some merch.

 Sponsor on GitHub  Shop Merch

Does PHP run in browser or server?

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!

Which browser is use in PHP?

PHP is a Server Side technology, so no browser, itself, runs it; instead, it makes requests and obtains from the Server the data and instructions to build inter-processes and pages.