Web Development: How does PHP work?

Daniel Pataki
Jan 8, 2009
Updated • Dec 29, 2014
Development
|
23

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.

Summary
Article Name
Web Development: How does PHP work?
Description
The guide explains how PHP works and how it differs from scripting languages such as HTML.
Author
Advertisement

Previous Post: «
Next Post: «

Comments

  1. Aurelien said on July 5, 2011 at 10:43 pm
    Reply

    Hi Daniel,

    Brilliant and simple explanation of how PHP works! I actually get it now :)

    And I’m not sure what bee stung George Glass…lol!

    ;)

    Aurelien

  2. praveen gupta said on April 7, 2010 at 7:36 pm
    Reply

    i am get for how php of work

  3. Daniel Pataki said on January 9, 2009 at 11:12 am
    Reply

    Hi G Thompson!

    Thanks for your insight! I always believe that you should find the right tool for the right job, as you said. What I don’t understand is PHP and MySQL is NOT complicated, I would say javascript is on the same level. So if soneone is ok with javascript they can easily learn any of the two.

    I agree with your comments that PHP is more powerful, although PHP + JS is also a great combination for doing AJAX-y stuff.

  4. Paul. said on January 9, 2009 at 7:30 am
    Reply

    Thanks again,Daniel, for this christal clear artikel.

  5. G Thompson said on January 9, 2009 at 6:59 am
    Reply

    continued…

    As for mySQL or any SQL RDBMS they are all ok. Sometimes Firebird is called for, sometimes MS SqlServer, sometimes Oracle, sometimes mySQl, Sometimes even a smallish flatfile db (maybe even an old dot.dbase 4 file ~memories~. Its up to the situation and what is needed for the specific project.

    Not one database is better than any other in EVERY application.

  6. G Thompson said on January 9, 2009 at 6:52 am
    Reply

    Gawd the standard broohaha about what people think HJavascript is all about. Having been a programmer since the early 1990’s in everything from VMS, PICK, Pascal, Cobol ~shudders~, ANSI-C, and then into the OOP side of C++, Python, Java.. as well as the server side uncompiled languages of Perl, PHP, C# etc.. And also having taught the newer variants of these in University and the way they interact with each other and SQL (as well as flatfile) DBM’s I can unequivecally state that Javascript cannot do one tenth of what a programming server side language can do. Especially when it comes do databases there is no ability to create unique session identifiers that hook between different pages in a secure way. Javascript is a quasi language, even its name says it all. It is a SCRIPTING language only like ASP (not the .net variant) and while it can do basic linked list structures it can never perform into a multi dimensional matrix way that is required in a fully relational database. Javascript is great for what it is used on the client side. But it will never matchthe securability, hookability, or speed of a server side “quasi compiled” programming language. I myself recommend learning PHP, Perl, and ASP.NET to all students.

  7. Dels said on January 9, 2009 at 3:31 am
    Reply

    I think in future Web Development have to be in own subpages of this site and then we can categorize it into several section:
    – HTML & CSS (Web Design)
    – Server Side Programming
    – Database Operation Related
    – Client Side Programming (JS, Ajax, etc)
    – SEO

    then it would be perfect
    (sorry that was just my Utopia LOL)

  8. George G said on January 9, 2009 at 3:29 am
    Reply

    Daniella,

    Comrade, we now live in brave new world where innovative companies are free to offer all sorts of interesting services and options. There are also magnificent invention called search engine to help you find these kinds of stuff. Good luck my friend, give my best to all your brethren herd… moo moo!

  9. Anonymous said on January 9, 2009 at 3:29 am
    Reply

    Daniella,

    Comrade, we now live in brave new world where innovative companies are free to offer all sorts of interesting services and options. There are also magnificent invention called search engine to help you find these kinds of stuff. Good luck my friend, give my best to all your brethren herd… moo moo!

  10. SK Mezanul said on January 9, 2009 at 3:26 am
    Reply

    Thanks Daniel for this article.

  11. Daniel Pataki said on January 9, 2009 at 2:35 am
    Reply

    Hi Womble, thanks a lot, I hope to keep them coming :)

    Gearge, I think I’m not arguing anymore :D Feel free to use Firebird, but I seriously doubt you can create a webpage with it unless you have your very own server, since no host will support it. It might be good technology though, I’ll take a look at it!

  12. Womble said on January 9, 2009 at 2:24 am
    Reply

    Another excellent insight Daniel, In a world of copycat blogging it’s always a pleasure to read your fresh articles.

  13. George Glass said on January 9, 2009 at 1:49 am
    Reply

    You are painting yourself into a corner my friend with your arguments becoming ever more increasingly ridiculous. I might as well tell you in turn that you should only program in C, since that is what PHP is built on. Regardless, there are many ways to connect to a DB with JS/AJAX. I was illustrating my point with a simple one. Besides, were you aware that you can even use JS as a server side language?? JS is really a much more advanced and capable language, under the hood it acts in a functional manner, so has more in common with powerhouses like LISP, despite its C syntax. In the future only those accustomed to programming in a functional method will be able to take advantage of multi-core computer resources…an added bonus!

    Also I begrudge your choice of MySQL as database and recomend users check out Firebird. It’s a slightly more difficult to start using, but has better performance and does not require the endless amount of tweaking, configuring, and learning that MySQL does for appplications of any scale.

  14. Daniel Pataki said on January 9, 2009 at 12:58 am
    Reply

    Sorry mate, but NextDB is also based on MySQL and PHP, and you need an account with them to use it. The reason it’s just JS is that it is a way for people who don’t know to create dastabases. The backend just translates your JS into PHP and MySQL.

  15. George Glass said on January 9, 2009 at 12:27 am
    Reply

    Daniel,

    I suggest you close your mouth and stop letting all the flies out.

    OH, and take a look here: http://www.programmableweb.com/api/nextdb

  16. Daniel Pataki said on January 9, 2009 at 12:18 am
    Reply

    Oh, sorry about the typo!

    Hi George!

    I think you should brush up on your programming knowledge a bit :) Javascript can not interact with mysql databases, since one is client side (js), the other is server side. You can pass variables from one to the other (php to js).

    Also, php indirectly is parsed by the browser, since it outputs HTML code, or even javascript actually. I use php to generate dynamic javascript stuff. Anyways, in the end you always get a “static” page, so the end result is the same as if you would’ve coded the page only in html and js

    Also, there is absolutely NO WAY you can build a big site without server side programming, how can you deal with user profile for example?

  17. Beermeister said on January 8, 2009 at 11:21 pm
    Reply

    Oh. A new term coined by ghacks? And here I thought it was just a regular old typo.

    1. Martin said on January 9, 2009 at 12:02 am
      Reply

      It is a misunderstanding. Daniel was explaining web development while you mentioned the typo. So, yes, it is a type and I corrected it ;)

  18. George Glass said on January 8, 2009 at 11:16 pm
    Reply

    Server side programming is over-rated. If you are designing content for the web then you should make it a priority to learn languages that will be parsed by the browser. In fact, now that javascript is capable of interacting with a database the average author has very little need for php.

  19. Daniel Pataki said on January 8, 2009 at 10:40 pm
    Reply

    Hi Beermeister!

    Yep, take a look at
    https://www.ghacks.net/2009/01/02/web-development-standardizing-variables-to-code-faster/
    for the announcement.

    As far as I know Martin will create a sub-domain on the site for specialized articles like this in the future :)

  20. Beermeister said on January 8, 2009 at 10:20 pm
    Reply

    Wev Development?

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.