The two programming languages I would like to compare are firstly, PHP and secondly, JavaScript.
PHP (PHP Hypertext Preprocessor) is a young language when compared to the likes of Perl and C and other largely popular languages. It was started in 1995 by Rasmus Lerdorf initially as a simple set of Perl scripts he named ‘Personal Home Page Tools’, later he expanded it and implemented it in C and then released it as an open source code base for all to use (The PHP Group, 2010). It has since then evolved from a procedural language to an OO language in its PHP 5 release (July 2004).
It is strictly a server side language which means the compilation, translation and actions are performed on the host computer it is running on. Syntax is similar to C/Java but it does not have certain rules of the other programming languages (such as declaring a variable as a certain type before using it, or the need to actually declare variables before using them). PHP’s purpose is predominantly for web applications but it can be used for non-web application scripting (command line tools etc.) although, from my experience, it is not well suited for background processing as it does tend to use up far more resources than languages like Perl and Python.
JavaScript is a language that was developed by Netscape to run on Netscape browsers and despite its name, is not a subset of the Java language or developed by Sun (Crockford, 2001). It is technically not an OO language, although it does have functions and objects it does not group entities into Classes. JavaScript is a client-side language meaning that it uses the client computers resources and compiler to execute its instructions, this makes it very light on the load of a server but also puts performance in the hands of the hardware that the users are accessing the script from, which, of course due to the nature of JavaScript being for web pages, is probably the most broad spectrum one could want when programming for consumers.
Both langauges I feel are often misunderstood due to the fact that just about any technical person is able to write a simple script in PHP and JavaScript and call themselves programmers. Often due to lack of training and expertise, the languages are given a bad name.
Similarities:
1) Both languages are almost exclusively for the web and were created specifically for web use in the mid 90s.
2) Syntax styles are both based on C
3) Until only recently with PHP going full OO, both languages were not officially OO languages.
4) Both languages are platform independent (compiler or ‘runtime environment’ is required though)
Differences
1) PHP is server side while JavaScript is client side.
2) PHP makes use of classes while JavaScript only has constructors and functions.
3) JavaScript is used for a lot of visual effects and enhancements for web GUIs.
4) Users are able to disable all JavaScript while browsing the internet due to its client-side compilation.
A simple example to outline the server-side/client-side JavaScript and PHP differences would be form validation. If you implement validation on a web form using JavaScript (eg: not allowing users to enter in an invalid email address or leave their name blank), the user is able to bypass the security checks by simply changing their browsers options to disable JavaScript. In PHP, if you validate form fields, the user must first submit the form to the server and the server will decide whether the data passed to it is correct or not, there is no way for the user to disable this from their computer.
An example of how similar the syntaxes are can be shown in the basic function below to convert a list of names to uppercase.
PHP:
function convertUpper($names) {
for ($i = 0; $i < count($names); $i++) {
$names [$i] = strtoupper($names[$i]);
}
return $names;
}
$names = array(‘michael’, ‘john’, ‘samantha’);
$names = convertUpper($names);
JavaScript
function convertUpper(names) {
for (i = 0; i < names.length; i++) {
names[i] = names[i].toUpperCase();
}
return names;
}
var names = { ‘michael’, ‘john’, ‘samantha’ };
names = convertUpper(names);
References
Crockford, D (2001) JavaScript: The World’s Most Misunderstood Programming Language [Online]. Available from http://www.crockford.com/javascript/javascript.html (Accessed: 10 October 2010).
The PHP Group (2010) History of PHP [Online]. Available from http://www.php.net/manual/en/history.php.php (Accessed: 10 October 2010).