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.
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.
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.
Note: This course was recorded with Adobe Dreamweaver, but you can use the code editor of your choice to follow along.
- How to use PHP in a webpage
- Storing and displaying text and numbers in variables
- Using functions
- Adding comments to your PHP scripts
- Moving common page elements to include files
- Displaying different content on a page
- Working with multiple values in arrays and loops
- Getting form input sent by the POST method
- Using PHP sessions to preserve data
- Storing data in session variables
- Connecting to and querying a database
- Handling errors