Web Development: How does PHP work?
When someone starts learning web development they usually start with HTML and CSS, and many people get stuck there without ever experiencing the wonderful work of server side scripting like PHP or ASP. I'll be talking about PHP here, but the basic rules for ASP and others are the same though.
The most important thing to understand is the difference between HTML and PHP. In HTML you write your code, upload it, and the user's will subsequently download that page along with all the code. The user's browser interprets this code and shows the user the page as you intended it (hopefully). In other words HTML is sort of what you see is what you get, in the sense that all the code goes to the user and is interpreted by the browser.
With PHP it works a bit differently because you don't actually download the code the author wrote. What happens is that if you want to download a php page the code in that file is first processed by the server, and you download the output of the code, as opposed to the whole code as is. This in turn will be HTML just as before, this is why you never see PHP code in the source of a webpage. So what happens in processing? Turn the page to find out!
With PHP the goal is to use the processing powers of the server to build (usually) dynamic webpages. A very basic example is showing the correct greeting for the time of day on a webpage. In human terms you write a script with the directions to show "Good Morning" if it is before 10am but after 5am, "Good Afternoon" if it is after 10am but before 6pm and "Good Night" after 6pm, before 5am. Instead of receiving all the code for this and processing it in your browser, this all gets processed before you download it, and you only get the result of the process, the text "Good Night" for example if it is 9pm.
This is much quicker, since if you think of bigger sites, instead of having to download 300kb (or much more) of code, it is quickly processed on the server and you might get as little as 10kb or less. Obviously your PC could process the code quickly, but downloading and handling can take a while. In addition, the code may also have database queries which can not execute if processed on your PC, they have to be processed on the server which has the database.
If you would like a more real-life example, take a look at gHacks, which has almost 5,000 posts now. In HTML world we would have to have 5,000 posts which all have the whole site code, from header to footer with the article in between. PHP makes it possible to "compress" those 5,000 files into only 1!
When you view any gHacks post on a single post page you are actually viewing a file called single.php. This file also has some additional info in the url which will tell the script which post to show, so the file you are viewing would be single.php?p=234. This tells the script that the post with the ID of 234 needs to be shown. The script queries the database for the relevant post and pulls its details (like title and post body) from the database. So in the end all you are shown is one post. WordPress has some other stuff built in to make nicer URL's and so on, but under the hood this is what is happening.
Likewise for the front page we don't always go and modify the code when posting something. Martin would be coding all day, removing the last post on the page and pasting the code of the new one. Instead, in the php file you are viewing the code gets the latest 10 posts and puts their data on the page.
There is a lot more to learn in PHP, but those are the basic mechanics, the ability to create pages based on certain criteria, as opposed to static content on each page.Advertisement