Lesson 1 - Introduction to PHP and web applications
Welcome to the first lesson of the PHP online course, which is designed for absolute beginners. We're going to create simple dynamic web widgets such as mail forms, polls and guest books. We'll also show you how to generate websites into an HTML template using PHP. If you already have a decent amount of knowledge of HTML and CSS, you are ahead of the crowd. If not, this course may be a bit difficult for you, but you'll surely be able to make it anyway.
PHP is being developed and consequently, changes frequently. This course is modern and is written to teach you how to program safely and correctly. We will also prepare you for upcoming courses, which are focused on advanced topics such as object-oriented programming and the MVC architecture.
Before we start programming, we'll go through a basic introduction to web applications. If you're going to work with PHP, you might as well know how it works first
Advantages of web applications
It's no coincidence that most apps nowadays are web apps. Mainly because web applications have many advantages over the classic (desktop) ones. The technology is more standardized and portable, and they usually generate more money and gain many more users.
In order to understand all of these benefits, let's take a small step into the history and origins of web applications.
When computers first began to spread amongst people and companies, they were really expensive. It was unimaginable to have a personal computer for each employee. For that reason, companies would only buy one central computer (which was substantially powerful for those days) and connected several keyboards and displays to it. All of the employees would work on the same computer at the same time.
If we take a word processor, for example, each employee would've had his/her own folder on the mainframe with his/her documents. However, all of them would use the same word processor that was installed on the mainframe.
The benefits of this architecture are:
- Easy administration - To make all users able to use a single application, we would only have to install it on the mainframe. At that moment it would be available to all of the users. The same goes for updates - we simply update the mainframe and all employees are instantly using the updated version.
- High security - All of the data is saved in the mainframe database - not at the employees' stations. The same goes with applications, no one is able to access their code.
Of course, there are also a couple of disadvantages:
- Low performance and high price - The mainframe has to be very powerful as it processes all of the users' requests. It's very expensive and has a high maintenance cost.
Mainframes are rarely used nowadays, but there are still some corporations out there putting them into use.
Later smaller businesses and even households began to purchase computers, as they became cheaper. Everybody could afford to have a personal computer. The mainframe era ended and every employee, or just overall user, had his/her own computer with his/her own applications.
Back to the word processor example, each user has his/her documents and processor on his/her own computer (desktop).
The benefits of desktop applications are:
- High performance - All performance is provided by the client's computer. Sometimes it may even be that the client is of the "fat client" type in this context. A fat client is when an application communicates with the server and only cares about sending data and doesn't perform rendering forms to the client, all of that is done by the application.
The disadvantages are:
- Complicated administration - If we release an update for our application, we would have to make sure that each and every client updates his/her computer. Which is ridiculous and highly improbable, so we'd have to deal with old versions. Users generally don't like downloading and installing updates or new software.
- Low-security level - Due to the fact that the user has access to the entire application, he/she can try to disassemble it (crack it), which isn't that complicated thanks to today's high-level languages. The user would be able to steal parts of our source code and/or data.
As the Internet expanded, lots of static websites were created. I'm assuming you already have some sort of experience making websites. Basically, a static website is a text file where you select parts of the document and mark them as titles, paragraphs, images, and so on. The finalized website, back then, was static. Meaning that it could not be changed. All it would be is a read-only electronic document.
HTML, the markup language used to assign meaning to website elements, files are simply saved on the server. When the client (the user with a web browser) sends a request to the server, the server simply responds by sending him/her that exact page. We call this architecture client-server.
As you may have already noticed, this architecture looks a lot like a mainframe. Let's go over its pros and cons:
- Low-performance requirements - The server only sends HTML files and doesn't have to display them or control the user's keyboard, display, or anything. It's all handled by the user's personal computer.
- Easy administration - When we change something on the server (add a document, for example), all of the users would be able to see it instantly.
- High-security level - If we wanted to restrict clients from accessing certain documents, they would have no chance of getting them since they're stored on the server.
The only disadvantage is that the server can only send static HTML files. There is no way to send data to the server and get a response based on that data. For example, sending your opinion in a discussion, voting on a poll, or to displaying what day it is.
After the huge spread of the Internet and web pages, people started to look for ways to add dynamic functionality to their websites. These attempts went so far, that nowadays we're able to achieve the same behavior on our web application as we would on our desktop apps. For example, the desktop version of MS Word, and the Office 365 or Google Docs web applications. We call these websites web applications.
The way web applications work is mainly through the client requesting specific documents from the server. Whereas on the server, there would be a CGI script running (a program that generates HTML files based on the user's request). The website would no longer be a simple file on the server, but a dynamically created version of itself each time a client's requests it (based on the actual request). PHP is the most popular language used for developing web applications.
The web-application interaction scenario often looks like this:
- The user enters a website address (URL) inside his/her web browser (for
eshop.com/printer-epson-123) and by doing that, he/she sends a request to the server.
- The server calls the PHP module.
- The PHP module handles the request (in this case, the user wants to display information about a printer), connects to the database and loads the corresponding data. Based on that, it generates an HTML file.
- That HTML file is sent to the client - where he/she would only be able to see a static website. Nonetheless, the website was actually dynamically created based on his/her request.
All of the data is stored in a database, where the web application (an IT e-shop in this case) has an administration interface through which employees are easily able to add new products, edit prices etc. If all of that data wasn't stored in the database, each product would have to have its own static HTML file (which would make it extremely hard to manage the pages). Also, without databases, there would be no way to filter products by price or write reviews about the products and so on.
Now that we've covered how dynamic websites work and also showed a short example, let's go over the pros and cons of web applications:
- Easy administration - Once we upload a new version of the application, all of the users would instantly be switched to the new version.
- High security - Both the website and the database are on the server. Unless they contain a security bug. Either way, it's really hard to steal a web application.
- Huge user base - People are lazy and don't like downloading and installing applications. The cool thing is that all they have to do to use a web application is click on a link and they would be able to use it straight-away. If the same application was released as a desktop app, it wouldn't have as many users. Remember, applications are intended for users! No matter if they're free or paid, we always want as many people to use them as possible. This is the main reason why most applications that are on the web last for years.
- High compatibility - Since we have access to the website through a web browser, we won't have to worry about the user's operating system and our application would work practically everywhere (even on smartphones).
- We would also have the advantages of having client computers because the server isn't exhausted by displaying the website, the web browser does that for us.
We can see that web applications are the future, and being able to create them is a great advantage when looking for employment.
The only disadvantage worth mentioning is that their development is a bit more difficult than the development of desktop applications. Mainly because the Internet was originally designed for static documents, so there are some limitations that we will have to workaround. However, web technology is ever-improving and a lot of things are way easier to do now than before. Mainly thanks to new web standards like HTML 5 and CSS 3.
That would be all for this brief introduction. In the next lesson, Installing Apache + MySQL + PHP on Windows and making an app, we're going to install PHP and create our first web application.