I know that HTML is viewed on the client side and PHP is on the server side. I guess I want to know why they use different things. Why doesn't the client just view a php file?
The PHP file needs to be processed and turned into HTML, because a web browser only interprets HTML, not PHP code. Essentially, a PHP file is a recipe for processing data into HTML that can be processed by a browser.
Because the client doesn't have access to your server :) HTML is the waiter and PHP is the kitchen. You don't want your users going into the kitchen - you want them to order food from the waiter to be prepared by the kitchen.
Well PHP runs functions and commands on the server-side before any HTML is even sent to the browser. HTML is the language that outlines where everything goes when the client loads the page and PHP is pretty much used to fill in all the information in those elements.
Edit: I like Dan Heberden's analogy better. :)
An HTML file is file that contains markup and it is a way to structure content. It exists to display data.
PHP and all other languages exist to process data.
They serve two different purposes.
Of course PHP and such can generate output that can be displayed. But a web browser is just not able to process PHP. The PHP script might also contain information or functionality that should not be visible to the client.
It might access a database, that can only be access from the server it is running, etc.
But if you really want to, you can create a web browser or plugin that executes PHP ;)
PHP is a scripting language that specializes in producing HTML as its output. When you request a .php file with your browser the server recognizes the extension and executes the script which then returns its output to the browser. because the output is HTML the browser can then render the site. the browser knows nothing about PHP.
In general PHP can be used just as any other scripting language it just has special support for generating HTML and there are servers (such as APACHE) that have support for executing PHP.
PHP is like the company that generates the HTML, so you have a PHP script that does a lot of tasks, consulting a database, filtering stuff, adding things, and after it completes the task, it generates a cool little HTML code that is the one that the browser is able to render.
So ASP, PHP, Java, RUby and everything else, works on the server to generate a HTML, so your browser can identify it
Why doesn't the client just view a php file? Because the browser can understand only html.
Then why do we need php? We need to maintain lot of data of user and it needs to processes we cant have in a user's machine we place it in a centralised location and that is obviously the server and it is also very well harnessed.
The request from client is for a PHP file, which is not served directly by the server.
The server instead processes it, as it is a "server-side" script and the PHP engine (again on the server) spits out HTML for the PHP code. Which is the "Response" from the server for the request the client generated!
If you're running a bank, do you want to have tellers perform transactions for customers, or do you want to let everyone walk into the vault, take their money, and trust them to write down the transactions themselves?
If you're using any kind of database or server-side files, you really need to be doing that processing server-side. If you're just permuting user-supplied parameters to mangle your content, then client-side javascript should suffice.
I don't completely agree with Dan Heberden's analogy, so here's my "corrected" version (IMHO)...
- DNS = host/hostess
- Shows you where to go
- Web server = kitchen
- It's where all the dirty work goes down, usually out of sight of the customers
- Web browser = waiter/waitress
- Passes your order to the kitchen, and later returns with your food
- HTTP = ticket/order
- Standardized the ordering format, to make waiter-to-chef communication easy
- PHP = chef
- Generates content and structures it with HTML
- Database = fridge/pantry
- Where all the ingredients are kept, organized and easy to access
- HTML = plate/bowl
- Provides structure for content
- CSS = presentation
- Makes your content appear attractive
- Content = food
- It's what you came for in the first place
- JavaScript = your food running around on the plate while you chase it with your fork
- Provides behavior to your food - this may seem odd to western cultures ;)