This is a apples to oranges comparison. PHP is an older language, running behind the Apache Web server in a request/response fashion. node. JS is a non-blocking event-loop framework running JavaScript within the V8 engine, with an optional Web server built In. Then again, was it really an apples to oranges comparison? Both Technologies is commonly used to serve webpages to browsers.
If you've been following my blog through the years, you're ' d know that I ' m a big PHP fan. I earned my PHP5 zce (Zend certified Engineer) certificate a few years back. I ' ve built a couple hundred content-based websites using various content Management Systems, as well as a dozen or so apps Using different PHP frameworks. I had a blast at the Zendcon, and I ' ve even taught a PHP meetup for about nine months.
If you've been reading my blog as of late, you'll also notice that I won ' t shut up about this new thing called node. JS (Br Owser-less JavaScript). Surely, I Haven ' t thrown away my years of PHP experience for this new kid on the block, right?
Honestly, I ' ve been using both languages recently. At work our websites is built using PHP (although I ' m finding more reasons for node. JS each day). For my side projects, I ' m writing multiplayer the Web games and hardware interfacing software using node. js.
Both environments have their pros and their cons, and neither language are the perfect solution for every project. In this post I ' m going to compare and contrast the environments, covering their strengths and weaknesses, and outline Which is better for various situations.
Why am I only covering Php+apache and node. js? Honestly, they ' re the language I know best and has production code running for each. Ruby and Python is some great languages in this same space with comparable strengths and weaknesses, but I never had the opportunity to learn them. JavaScript (node. js) and PHP both has syntax influenced by C.
Strengths of PHP
PHP is by far the most widely used Server-side Web programming language. It's old, and there are never short supply of cheap, shared hosting providers. Some of the largest and commonly used Platforms/apps use PHP. WordPress, the most popular of self-hosted blogging platforms are PHP. MediaWiki, Joomla, is some more common self hosted apps which use PHP.
Some of the biggest websites use PHP, such as Facebook, Wikipedia. PHP uses traditional (read, familiar) Object oriented methodologies. There is loads of PHP web frameworks and language documentation.
PHP is great for serving up content websites. PHP sits behind a Web server, which can check to see if the file being requested exists in the filesystem. If So, the file can is served to the client without needing-to-run any PHP code. This isn ' t necessarily a pro in the language itself, but it's a beneficial side-effect for most situations.
PHP also have corporate backing by the Zend company (their tagline are "The PHP Company"). This backing was required by big corporations, which all share the same philosophy, "If something doesn ' t cost money we don ' t want it.
Weaknesses of PHP
PHP is isn't meant to being run for extended amounts of time. Many'll argue with me here, but the language by default was set to terminate itself once it had been running for Secon DS, or if it reaches a certain amount of memory usage. This can being disabled, and apps can be built-to-run for a long time successfully, it's not where PHP shines.
The language isn ' t able to run code in parallel. You can, using tools like Gearman, pass off some work to is handled by other processes, and kinda keep a eye on the progr ESS, but the "process is rather klunky, and are not" what PHP was intended for. Gearman itself offers some other great features and can is used by many different environments, including node. js.
Back on the day, when URLs and Filesystems had a 1:1 mapping, it made perfect sense to has a Web server separate from the Language it is running. But, nowadays, any PHP app with attractive URLs running behind the Apache Web server was going to need a. htaccess file, WH Ich tells the server a regular expression to check before serving up a file. Sound complex and awkward with unnecessary overhead? That ' s because it's.
Overall, the stack required to support PHP was overly complex when compared to something simpler like Node. One needs Apache, which have some global settings as well as site specific settings. One also needs PHP, which has global php.ini settings, some of which can is overridden at run time (and not all). There is also a bunch of the old stuff left around which should being removed, e.g., Y2K support (finally removed in 5.4).
The official website is quite ugly and outdated. The docs is okay, the user contributed notes is very useful, however the method to update docs involves SVN and hacking Away at XML files, and are not exactly encouraging for most people to write docs.
Package management is virtually non-existant. Sure, there is PEAR, but the tool is ridiculously painful to use. Some Other package managers has appeared, e.g. Pyrus (PEAR2) and packagist, but usage are so scattered that there are no de Facto standard. There probably never'll be is honest. There is also phpclasses.org, but this site was painful to use and requires a signed-in user to browse.
Since a PHP process starts, does some boilerplate work, performs the taks the user actually wants, and then dies, data is Not persistent in memory. Can keep this data persistent using third party tools like Memcache or traditional database, and then there are the Ove Rhead of communicating with those external processes.
Strengths of node. js
node. js ' s biggest strength, IMO, is the It is event driven. Node apps run great over long periods of time. The event emitter code, while pretty in its core, provides a powerful and consistent interface for triggering code execution when needed.
Node has a Web server built in. Some people call this a bad thing, I call those people crazy. Having the server built in means so you don't have the awkward. htaccess config thing going on. Every request is understood to go through the same process, without have to hunt through the filesystem and figure out W Hich script to run.
The number one bottleneck with web apps isn't the time it takes to calculate CPU hungry operations, but rather network i/ O. If you need to respond to a client request after making a database call and sending an email, you can perform the A Ctions and respond
When both is complete.
Of course, with node. js being written in JavaScript, if you already know JS for frontend development, you are doing pretty Good in the node. JS Realm. You already know the syntax, and the gotcha ' s of the language, and the ' A ' is a API for interacting with the system.
The package management system, NPM, are great (although the website leaves much to be desired). Instead of having to follow strict guidelines and formatting requirements (e.g. PHP's PEAR), anyone can put anything into NPM (even me!). It's similar to the Android market. Sure, you can get the some crappy things out of the IT, but if one uses common sense they won ' t is downloading a virus.
Being so new, it doesn ' t has a lot of baggage leftover from the days of the old. Have a server built in, the stack is a lot simpler, there was less points of failure, and there was more control over WHA T can do with HTTP responses (ever try overwriting the name of the Web server using PHP?).
Data can is persisted in memory very easily, so if you is sharing data between different clients (e.g. with a multiplayer Game), this sharing of data are built in.
Weaknesses of node. js
node. JS is a very new, API unstable, untested platform. If you were going to being building a large corporate scale app with a long lifetime, node. JS is not a good solution. The Node API ' s is changing almost daily, and large/longterm apps would need to be rewritten often.
If you're serving up a lot of static files, such as images, you don't want to use node. js, otherwise you get back to the Situation where are checking the filesystem if things exists. This can is fixed by moving any static content to a subdomain or content Delivery Network.
JavaScript is a far from perfect language, has been created over a ten day period. If you is a traditional Class based developer, getting used to a functional programming language can be painful. Getting used to asynchronous programming can also is difficult.
The persistent memory thing can be a little tricky. If you don ' t know "what do you ' re doing, you might accidentally share data between clients (which could is a disaster). Also, you is in bigger danger of memory leaks. If you keep appending data to an array in PHP, the script is only really have a lifetime of 0.1 seconds, but your node. js SCRI PT needs to run forever, and you can easily blow it up over a period of time.
Node is a single threaded, even though it kind of appears to being multithreaded with the asynchronous execution it does. This doesn ' t really matter too much, when are the last time your app's biggest bottleneck was CPU instead of waiting on net Work I/O? So, while it would is cool to being multi-threaded, it doesn ' t usually harm an app.
Which should I learn?
This is a great question. If you do have a experience developing server side software, you should probably start off with PHP. It's a great, easy language for performing the request, response pattern which makes the stateless Internet great. You'll surely find more forum posts from people have the same problem as you (even though the posts might is years Old).
If you is looking to build other kinds of software, node. JS may be a safer bet. For example, I ' m writing some software which requires code to is triggered by changes made to hardware, such as a wireless Network coming into range. Writing This kind of software using PHP would was hacky and painful, but with node. js it is a breeze.
As far as being future proof are concerned, I am not convinced that either language would stand the tests of time. The future of Web apps is probably single Page applications, where smaller amounts of data is sent over the wire and the Frontend is in charge of generating HTML, through the use of websockets. PHP can ' t nicely does websockets like node. JS can. However, it's too soon to tell if node. JS would win the asynchronous, event driven language war. Due to the major flaws of JavaScript, a better language could easily kick its.
Example situations
Here's some quick examples of different programming situations and which language you should probably go with.
Is you building some sort of daemon? Use Node.
Is you making a content website? Use PHP.
Does want to share data between visitors? Use Node.
is a beginner looking to make a quick website? Use PHP.
is going to run a bunch of code in parallel? Use Node.
is writing software for clients to run on shared hosts? Use PHP.
Does want push events from the server to the client using WebSockets? Use Node.
Does your team already know PHP? Use PHP.
Does your team already know frontend JavaScript? Node would is easier to learn.
is building a command line script? Both work.
Simple equation
Here's a funny of looking at things ... To emulate the functionality of node. JS using PHP + Apache, you would need a couple other services running. To has Node act the same as PHP, you would simply run a bunch of synchronous code.
node.js≈php + Apache + Memcached + gearman-complexity
Related Posts
Why node. JS is awesome:a Short History of Web applications
http://www.bkjia.com/PHPjc/477984.html www.bkjia.com true http://www.bkjia.com/PHPjc/477984.html techarticle This is a apples to oranges comparison. PHP is an older language, running behind the Apache Web server in a request/response fashion. node. JS is a non-blocking event-loop framework ...