PHP vs JavaScript – What are the differences?

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).

 

RISC vs. CISC and Programming

CISC architecture is known as ‘complex instruction set computer’, this architecture is based on the argument that a CPU is able to perform large numbers of complex instructions and therefore should be used to do so, even if they are redundant (Brookshear, 2009, p.85). RISC architecture is known as ‘reduced instruction set computer’, this architecture is based on the argument that the CPU should have a minimal set of instructions due to the fact that once a CPU has a certain minimum amount of instructions, adding any more does not increase capabilities and will only decrease the speed and efficiency of the machine (Brookshear, 2009, p.85).

The strengths of RISC over CISC are very apparent when reading though the paper by George (1990). In his paper he states that while CISC is more popular and will no doubt remain so in the near future due to backward-compatibility for current software, RISC has far more advantages when going forward due to the “constantly changing body of knowledge used for processor design”.

From my readings I believe that CISC processing is best suited for RAD (Rapid Application Development). In this day and age, at the pace of the IT and software industry it will help us with concentrating more on breaking new boundaries in development of less-complex software and design by taking the tedious work of programming operations that are run-of-the-mill requirements for every day applications away and allowing us to concentrate on more complex and new developments. Issues that may hamper this, however, is due to the fact that CISC processors are less efficient and take longer to process than RISC, and speed of execution is one of the most important factors in software today.

RISC processors allow us to have almost a “clean slate” when programming. With its minimal instruction set it can cater for new methods of developing the simplest of operations at a higher speed of what has been discovered and pre-programmed into the CPU like in CISC processors. RISC will also allow for more complex and low-level (more than likely larger, more advanced systems) development at more efficient performance due to the lack of excessive and potentially un-used instruction sets slowing down performance.

As far as future replacements, the popularity of the CISC chip may be replaced by the RISC chip as stated in the paper by George (2009). I do tend to agree on this but also by perhaps adding the ability for custom instructions to be stored in the CPU which would allow user/operating system/software defined instructions to be added dynamically for optimisation of that particular system configuration

References

Brookshear, J.G (2009) Computer Science: An Overview. 10th ed. China: Pearson Education Asia Ltd.

George, A.D (1990) “An overview of RISC vs. CISC”, System Theory, 1990. Twenty-second Southeastern Symposium on, 11-13 March, Cookeville. IEEE, pp.436-438.