From the course: PHP for Web Designers

How PHP makes web pages dynamic - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

How PHP makes web pages dynamic

Before getting down to the details of how to use php, let's take a look at how it makes web pages dynamic, and how it differs from JavaScript. When someone visits a website, the browser sends a request to the webserver. Which responds by sending back the HTML images, style sheet and other resources for the browser to display. If the page contains interactive features such as an accordion or tab panels, the JavaScript that controls them is sent along at the same time. Either in a separate JavaScript file or embedded in the HTML. This HTML page contains an accordian widget. As long as the page remains open in the browser the JavaScript sits patiently listening for click events. As soon as I click on the heading of a panel, the JavaScript springs into action sliding the panels open and closed. Like JavaScript, PHP can be in a separate file. Don't worry what the code means at the moment, it'll become clear as you progress through the course. PHP code can also be embedded in the HTML, as it is on line 12 in this page. But that's where the similarity ends. When a browser requests a PHP page, the web server sends the page to the PHP engine, a piece of software that runs on the server. This processes is the PHP code and merges the output with the HTML. And it's this merged output that's sent back to the browser. The PHP code remains on the server and is never seen by the browser, or by the person requesting the page. Let's load this PHP page into a browser by running it through my testing server. I'll open a browser, and a new tab. And the address with be localhost/exercises/chapter01/01_01. And then PHP time the name of the file.PHP. The PHP engine processes the PHP code embedded in the HTML, gets the current time from the server and displays it. It also includes the external file copyrightphp which generates the range of years for the copyright notice, and if I view the page source. Let's just. Right-click, and view page source. You can see here, there's no sign of the PHP code. The time and the date range are simply part of the HTML. And this is a fundamental difference between the dynamic output created by PHP and JavaScript. Although JavaScript can send a request to the server, to load fresh content. Most of the time it runs entirely in the browser. The difference becomes obvious if we look at this page. If I scroll down to the bottom of the page, this JavaScript here generates the current time and updates it every second. I don't even need to run it through my testing server. I can just open it directly in a browser and the JavaScript goes to work. So if I open my files and then double click jstime.html, it opens in the browser and it's running the time. The time is constantly being updated. The JavaScript also listens for click events on these two buttons down here. So I can stop the clock. And if I click Start Clock, it starts it again, updating the time with the new time. You can't do that with PHP. Once the server has generated the output, that's it. That time is fixed. The only way to update the time with PHP is to send another request to the server. For example, by reloading the page. I've now got the new time, but it won't change until I reload the page again. PHP stands for PHP hypertext pre-processor. The name's a bit of a mouthful. But it simply means that the code is processed on the web server rather than in the browser. That doesn't mean PHP is less powerful than JavaScript. Far from it. One area where PHP comes in extremely useful is in it's ability to process input from online forms. If I enter my name in this form. Just type my name in there. David and then submit it. The page displays a suitable greeting. According to the time of the day. Admittedly, you could do something similar with JavaScript, but let's just look at the source code in the browser. Although the URL is still the same, the content of the page has changed completely. The form is no longer there. This is only a trivial example. But it has enormous implications. Using PHP, you can serve different content to people depending on who they are. Or if they're logged in. Another major strength of PHP is its ability to query a database. The PHP engine hands off the query to the database and populates the HTML with the results. Usually in a fraction of a second, ready for the server to send back to the browser. You'll learn how to do that towards the end of this course, but before we get there, you need to learn some of the basics of how to incorporate PHP code into your webpages.

Contents