tags:

views:

733

answers:

9
+2  Q: 

How does PHP Work?

We have all seen many question on StackOverflow that are founded upon the idea that PHP works like Javascript. Where the person clearly does not understand that PHP is a Preproccessor and only works before the page is sent out.

A clear example of this is in the following code, where runCommand() will not run when the user presses the button.

<a href="<?php runCommand(); ?>">Click Me!</a>

as it would in Javascript

<a href="javascript:runCommand();">Click Me!</a>

I've seen many questions like this that are from new people that just simply don't realize 'how' PHP works.

My question is: Where is a great resource that explains how PHP works?.

I want to be able to redirect people to a page that can get them going on the correct track and know what being a Preproccessor means.

(This also allows me to be lazy and not have to write an explanation every time it comes up, but don't tell anyone!)

If you don't know of a place that describes this well, feel free to provide your own interpretation.

As Carl Smotricz points out, there is a part of PHP that can be used outside of the browser. But I'm mainly talking about in a Apache enviorment where a user requests a web page, and expects to get something back, usually in HTML.

+1  A: 

The PHP code is interpreted on the server side an only the output of your PHP code will be send to the client.

So if a PHP file is requested, the web server sends the PHP code to the PHP interpreter, waits for the output and then sends the output back to the client.

Gumbo
+6  A: 

It could be that you're the one who does not understand how PHP works. PHP is a full language interpreter, and it's completely possible to run PHP scripts without a browser, outside of a Web server: On the command line or in an IDE or other GUI environment.

The PHP preprocessor of which you speak is only the function of an Apache module that calls on the PHP interpreter for this particular limited purpose.

Carl Smotricz
Most of the questions I relates to it in the use with a web server. I specified that in the question.
Chacha102
While this is true, the context which 99.9% of all PHP-discussions evolve around is server-side scripting. Server-side scripting is, after all, what PHP was designed for. Assuming that PHP == server-side scripting by default is very practical.
kusma
A: 

php responds to http requests in the typical server-side scenario. the browser reads this response and is responsible for rendering it and running any additional dynamic scripts embedded in the response on the client side. that is essentially the division of labor in that scenario.

jspcal
+13  A: 

Wikipedia is always a great resource of information. I suggest:

Server-side scripting

vs

Client-side scripting

Felix Kling
Add to that (it's old-school 'CGI' days, but could be a good primary for some): http://www.garshol.priv.no/download/text/http-tut.html
micahwittman
+1 for a simply answer to a simply question ;)
DaNieL
Also important is that JS in the DOM is event based, versus PHP's batch style. This is equally important for why the JS example works how it does, and I'm surprised nobody else here has commented on that.
Alex JL
+1  A: 

I'd point them to php.net. Specifically, the Getting Started section of the docs.

It has a nice section on what PHP can do. If they are really interested in php, php.net is probably the definitive place to get more info regardless.

Chris Lively
A: 

If someone thinks php and javascript both works same, then a low level explanation would be very complicated for them. Instead you should show them a comparison like this:

http://theopensourcery.com/phpcomparetojs.htm

Here author creates two same forms that do the same job, but one is with PHP and other one is with Javascript. The author also provides source code and points what is similar and what is different.

JCasso
This sounds kinda 'naive', php and javascript cant do the same things becose they are intended to be different. Its like to say 'taste good the chocolate, or is faster the train?' ;) And to compare two things so different can make think a newbie that they are almost the same, except for some *little* differences...
DaNieL
@DaNieL: If this is joke it is not funny, if not not go on and read => You did not read the link did you? It compares two languages by syntax and ends up with explaining servers side and client side stuff. And actually comparing PHP and Javascript is mostly like comparing chocolate and errr chocolate... You did not use ASP did you? You can write ASP with Javascript, and search for SSJS yes, Javascript that runs on server side...
JCasso
A: 

An important distinction is that JavaScript in a browser is event driven. That is why a click handler is not executed right away as the page loads, for example. The javascript could not be waiting to respond to that click either, if it was not for the event-driven style of dom programming.

I don't really think this is what is meant by the term 'preprocessor'. the client/server side distinction is more important. For instance, have you heard of any other server side language being referred to as a preprocessor when performing the same tasks as PHP?

Alex JL
A: 

In short, PHP belongs to the server, it usually then output HTML but it's not here for that (or at least, only for that). The user browser see only what remain after php did what it have to do.

Javascript belongs to the client (aka browser): it usually handle the DOM created by PHP. Javascript can change his behavior in base of the browser (everyone who wroted js scripst know about cross-browsers problem, do you remember IE6?) Javascript cant handle database all by itself; It have to rely on a sever-side language (php, maybe? ;)

BTW, AJAX can be a good reference to understand what exactly do php and what js do.

DaNieL