Sunday, December 03, 2006

What's the difference between PHP and Javascript?

PHP: Hypertext Preprocessor

For the past 10 years, I have concentrated my programming activities in JavaScript, a scripting language in which programs run inside a client browser. Since I have been focusing primarily on what the user sees on the web, this has served me well. But there were always limitations.

Recently, I have gotten interested in Wikis, which is a particular style of web that allows for both reading and writing. To support writing, it must have a content management system -- a system to hold the contents of a web page and which allows updating that content. I choose to focus on pmWiki because it seems to be a Wiki system which encouraged extensions -- which has turned out to be extremely useful in getting a basic mechanism to do what I want it to do. pmWiki is written in PHP, another scripting language. Unlike JavaScript, PHP scripts run on a server and can produce results (html code) that is shown in a browser.

So, why two scripting languages? Why isn't there a JavaScript that can run on a server? Why isn't there a PHP that can run on a client? For a long time, I couldn't figure out the answers to these questions. Finally, I think I have figured this out.

The differences have to do with the original purpose of the two scripting languages, and the major influences on each of them. In reality, there really isn't any difference between them -- or for that matter between them and Perl, Python and a bunch of others (see Scripting Language). The difference is due to the focus of JavaScript on running in the user's browser -- on the client side, rather than in the service provider's server -- on the server side.

If a script runs on the client side, then it must be prevented from violating the security of the user's computer: with client-side scripts, the script is downloaded from some other source -- whose motives are unknown. To prevent harm from being done, JavaScript has all sorts of restrictions placed on what can be done. One big one is, no interactions with the client computer's file system. This has such a big influence on the language that almost no one thinks of running JavaScript on the server side because it has no way to change the state of anything other than the bounded environment in which it runs. This is not because it couldn't be, but because the developers of JavaScript spent all of their creative energies working in an environment where they were just focused on activities they could perform in a web browser.

The developers of PHP focused their energies on all of the functionality you must have to be a general purpose programming language which can access all of the resources of an Internet connected computer system: they did not spend any time thinking about how to restrict PHP so that it could run client side.

And that is the difference.

I must admit that I am still somewhat surprised that someone hasn't made a bigger effort to develop a version of JavaScript that is fully competitive with PHP, Perl and the host of other server side scripting languages. There are some implementations of JavaScript that run on the server side, but they don't seem to be very popular -- PHP and Perl dominate server side scripting.

No comments: