Why PHP script is not workig in a web browser?

Viewed 17026

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.

9 Answers

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.

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.

In short, PHP belongs to the server, it usually then outputs HTML but it's not here for that (or at least, not only for that). The user browser "sees" only what remains after php did its thing.

Javascript belongs to the client (aka browser): it usually handles the DOM created by parsing the HTML, which is (possibly) produced by executing PHP. Javascript can behave differently in different browsers (everyone who has written JS scripts know about cross-browser problems, do you remember IE6?) Javascript can't handle database all by itself; It has to rely on a sever-side language (php, maybe? ;) (except if talking about node.js)

BTW, AJAX can be a good reference to understand what exactly PHP does and what JS does.

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?

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.

The PHP compiler executes in the server as a CGI script. A CGI script reads from Standard Input and writes to Standard Output, similar to a console or command prompt program. The PHP compiler reads the file containing HTML and embedded PHP and writes the HTML out and (when encountered) executes the PHP and writes the result of the PHP code. The output is received by the server then sent to the client.

See PHP: CGI and command line setups - Manual that says:

By default, PHP is built as both a CLI and CGI program, which can be used for CGI processing.

The difference between CLI and CGI are insignificant here.

Also see PHP: What is PHP? - Manual that says:

the code is executed on the server, generating HTML which is then sent to the client.

See RFC 3875 - The Common Gateway Interface (CGI) Version 1.1. It is the official definition of CGI. PHP seldom uses the original CGI protocol; other protocols such as FastCGI are used and perform the same function as the original CGI protocol but are designed to be more efficient. For the purpose of understanding how PHP works the original CGI protocol is relevant. The following is an excerpt of the RFC.

Abstract

The Common Gateway Interface (CGI) is a simple interface for running
external programs, software or gateways under an information server
in a platform-independent manner.  Currently, the supported information
servers are HTTP servers.
Related