tags:

views:

405

answers:

10

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?

+4  A: 

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.

mipadi
+44  A: 

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.

Dan Heberden
So who's the hostess?
webbiedave
Haha - the browser :p
Dan Heberden
If you get a PHP error page, is that like finding a fly in your soup?
Jeff
@Jeff: No, that's the waiter saying "No soup for you today!".
Tomalak
@Tomalak Or receiving an undercooked meal on a chipped plate :P
Jeriko
@Dan: Id' say HTTP is the waiter, PHP is the chef and the web server is the kitchen. And tagsoup is for dinner. :D
Tomalak
Lol - aren't analogies grand? Careful of IE, it's that table that tips over easy and has dirty silverware :p
Dan Heberden
It's too bad the experience between hostess means that you could get a seat next to the bathrooms with terrible service, or a quiet booth with service that is quick and compliant. Just need to pick the right night I guess?
Kris.Mitchell
Next time I go to a restaurant I'm going to ask the waiter for his <title> and a meta description of the specials.
webbiedave
Careful, they might want your HTTP_USER_AGENT
Dan Heberden
Waiter, there's a CSS bug in my soup!
Ben James
Waiter says, "No. It's a feature."
webbiedave
Very nice answer. As an example for the OP, I just finished a sitemap for a new site of mine. It has ~15 PHP files (kitchens) which "cook" and serve ~3,500 HTML files (dishes) from a MySQL database according to my new stats. Not bad, huh?
Andrew Heath
+1  A: 

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. :)

animuson
+6  A: 

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 ;)

Felix Kling
+1  A: 

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.

luke
Why has PHP special support to generate HTML? I'd say it is with all other languages, I just print a string. Nothing special.
Felix Kling
@Felix he is likely refering to the ability to easily intermingle HTML and PHP in templates, but I'm just guessing.
Gordon
@Gordon: Probably. But then I would not say that PHP is *producing* HTML.
Felix Kling
@Felix i was referring to how you can intermingle HTML and PHP code. what would be a better way of saying this?
luke
@luke: I don't know. Maybe they just play very well together ;) I just don't want that one, who is not so familiar with PHP, gets the impression, PHP is really *producing* HTML. In fact, I bet you could this intermingling with any language, providing some sort of indicator where to start (maybe `<?python` :) ).
Felix Kling
@Felix you certainly could do this in any language. just look at ASP.net with the embeddable code blocks (<%...%>).
luke
+6  A: 

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

camilo_u
+1  A: 

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.

nepsdotin
+1  A: 

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!

System.ArgumentException
+1  A: 

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.

Chris
+5  A: 

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 ;)
Dolph
Sweet! I've always wanted to own my own restaurant!
Dan Heberden
I disagree, JavaScript is more like the chef cooking it right there in front of you at your table.
animuson