Static and dynamic webpages
A static page or a dynamic page…What are you talking about? This might be one of the questions you’re asking yourself right now. Let’s clarify this mystery.
HTML is a language to display static pages. This means, the webpage will always say “Hello World, it’s Monday 9am!”, no matter whether it’s really Monday 9am or Monday 3pm or Wednesday 9am or Thursday 5pm to give you a few examples. Not very adapted, right?! 🙂
In our course, we will focus on PHP as an example for dynamic programming languages. PHP is a server-based programming language and produces HTML code that changes depending on the circumstances you have coded. Remember server means computer. In the case of PHP it means it is running on the same computer as your website. Why is this important to know? It has access to all information on the computer and thus allows you to do many things with PHP: You can communicate with your databases, manage cookies, create customized content for the web browser or analyse data sent from this web browser for instance.
Getting back to our example, it will show you the current date and time of your time zone. How does this look like with PHP? It can be written directly in your HMTL code (see extract below).
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>My website</title> </head> <body> <h1>My website</h1> <p>Today is the <?php echo date('Y/m/d'); ?>.</p> </body> </html>
You can see in the example above that
encloses a block that contains PHP. date is a PHP function that allows you to get the current date from the computer who is executing the PHP script. Within the PHP block Y stands for Year, m for month and d for day.
echo is also a PHP function; this one allows you to display something, the current date in our case. We will see more about echo and other PHP functions in the next article about the basics of PHP.
Please note that a index.html file would now be saved as index.php as it contains PHP blocks inside and this ending is needed to allow now that the code is correctly interpreted by the PHP interpreter. A PHP interpreter you may ask?
Yes, PHP is not only a programming language, but it is also an engine that interprets the language into an intelligible language for browsers. HTML can be directly interpreted by browsers. A .html file can be opened directly within a browser such as Firefox or Google Chrome. Unlike with HTML, browsers are not able to understand PHP without prior interpretation. We need a web server, like Apache, that is equipped with a PHP interpreter to transform the PHP file into a HTML one. This interpreter communicates with the server and sends him, after his initial request, the transformed file. This new HTML file will then be sent from the server to the client, the browser who made the initial request.
All in all, this is what’s behind a dynamic page. Simple, isn’t it?! But that’s not all: The revolution of a dynamic page is that the page the user gets is just built at the very moment of the user’s visit.
How does this work in general for a webpage? The content of a website is split into several packages. A content like a footer or menu is just created once and will be used for all pages where it is included. Not clear yet? Let’s see how it works for static pages: A website consists of several different pages, all created physically. This means there is a page home.html, contact.html, presentation.html for instance. All of them include a menu and a footer. If you would now like to change the layout of your menu or the content of your footer, you would need to change this in all 3 pages separately.
You can now guess the advantage of dynamic pages? Correct, you only need to make one change for all pages in the relevant file such as menu.php or footer.php.
Another advantage of a dynamic page is that you can do programmed updates without human interaction. What does this mean?
Let’s take an example: A newspaper website like the New York Financial Times cannot be written in HTML/CSS only, as it needs to be very reactive to when news appear. This means the website needs to be updated on a regular basis and also very quickly without a lot of testing as the news need to get out to the world. Therefore, programmed updates are helpful to save developers’ time and put news online very quickly ensuring that no other part of the website is harmed.
Now you might be eager to discover how PHP makes this all happen. Let’s check this out in the next article introducing the basics of PHP.