18

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.

mip
  • 8,355
  • 6
  • 53
  • 72
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150

9 Answers9

28

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

Server-side scripting

vs

Client-side scripting


And Wikipedia also has pictures:

enter image description here

hakre
  • 193,403
  • 52
  • 435
  • 836
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 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 Jan 09 '10 at 19:01
  • 1
    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. – JAL Jan 09 '10 at 20:36
  • @JAL PHP is not event-based because there are no events to process when PHP executes. – Sam Hobbs Mar 25 '21 at 19:35
10

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
  • 66,391
  • 18
  • 125
  • 167
  • Most of the questions I relates to it in the use with a web server. I specified that in the question. – Tyler Carter Jan 09 '10 at 19:00
  • 2
    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 Jan 09 '10 at 19:36
  • I agree, the preprocessor part is somewhat anachronistic at this point and mainly is in the name to make the acronym work. – JAL Oct 08 '13 at 15:04
2

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
  • 643,351
  • 109
  • 780
  • 844
  • Oh, can i have a sample of HTML with the "code behind"? I learn asp.net, html and code behind are on different "pages" :x – CodeGuru Jul 27 '12 at 15:06
2

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.

Milind R
  • 676
  • 1
  • 8
  • 20
Strae
  • 18,807
  • 29
  • 92
  • 131
1

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?

JAL
  • 21,295
  • 1
  • 48
  • 66
0

PHP is server-side scripting language which means all php code is executed before page is sent to the client side. For that reason you will never see

<?php ... ?> 

in page source.

On high abstraction level... You can consider web server (hardware) as component of four different parts. Webserver(software, for example Apache), File system, database and PHP plugin.

So for example when you sent page request (for some page .../example.php) to the web server Apache will try to find that page in file system and if the page exists he will call php plugin to executes all

<?php ... ?> 

code (of course including db queries). After that page is sent back to the client side where you can manipulate with page through JavaScript, designed it through CSS...

More on: https://www.youtube.com/watch?v=PemsuAfc7Jw

Srle
  • 10,366
  • 8
  • 34
  • 63
  • In general, PHP IS NOT ONLY A SERVER-SIDE scripting language. Please read http://stackoverflow.com/tags/php/info, "SAPI support" section. – mip May 06 '14 at 18:58
0

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
  • 50,847
  • 7
  • 72
  • 76
0

The reason why PHP scripts are not working in a web browser is only because web browsers do not support PHP (at least I don't know any). This fact is not as trivial as one may think.

And it may sound disturbing, so take a look at HTML specifications on W3C website of HTML 5 and HTML 4.01 (because it has more verbose examples). What you can find? That scripts can be written in languages other than JavaScript!

Here is an example from HTML 4.01 documentation (section titled Specifying the scripting language).


(...)

Here's a more interesting window handler:

<SCRIPT type="text/javascript">
      function my_onload() {
         . . .
      }

      var win = window.open("some/other/URI")
      if (win) win.onload = my_onload
</SCRIPT>

In Tcl this looks like:

 <SCRIPT type="text/tcl">
     proc my_onload {} {
       . . .
     }
     set win [window open "some/other/URI"]
     if {$win != ""} {
         $win onload my_onload
     }
 </SCRIPT>

Script written in Tcl is perfectly O.K. in HTML! What about PHP? HTML5 documentation says:

A user agent is said to support the scripting language if each component of the script block's type is an ASCII case-insensitive match for the corresponding component in the MIME type string of a scripting language that the user agent implements. (...) User agents may support other MIME types for other languages, but must not support other MIME types for the languages in the list above. User agents are not required to support the languages listed above.

Thus it is only up to web browser (user agent) if it is going to support PHP or not. Playing with W3C example, PHP aware web browser might have accepted something like this.

<script type="text/php">
      function my_onload() {
         . . .
      }

      $win = $window->open('some/other/URI');
      if ($win !== false)
          $win->onload = 'my_onload';
</script>

So, the reason why people ask such questions is not that they don't know how PHP works. It is because they don't understand web technology in general. They fail at point, which requires understanding of what, where and why is supposed to be executed.

mip
  • 8,355
  • 6
  • 53
  • 72
  • The problem is not that web browsers do not support PHP, the problem is that PHP does not support web browsers. PHP is designed to execute server-side and therefore works totally different from client-side. – Sam Hobbs Mar 25 '21 at 19:25
  • @user34660 not true at all. Read the last paragraph of my answer. – mip Mar 26 '21 at 16:05
0

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.
Community
  • 1
  • 1
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32