Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Wev Development: How does PHP work?
Closed Thread
Old 01-08-2009, 03:39 PM   #1 (permalink)
Osiris's Avatar
 

Join Date: Jan 2005

Location: Kentucky

Posts: 32,054

Osiris is a jewel in the roughOsiris is a jewel in the roughOsiris is a jewel in the rough

Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris Send a message via Skype™ to Osiris
Default Wev Development: How does PHP work?

Wev 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 workd 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.
__________________
Osiris is online now  
Old 01-08-2009, 04:31 PM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Wev Development: How does PHP work?

That really didn't explain how PHP works in the slightest.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and Flash Lexluethar Programming Discussions 6 07-23-2008 12:44 AM
Bed-ridden work via s-video to TV (PLEASE HELP) fixedrock Building, Buying, Upgrading for General PCs 3 07-01-2008 11:44 PM
USB Mice Don't Work - Everything Else USB Does. Blizzard596 Windows Operating Systems and Software 2 03-26-2007 06:35 PM