Image Source: pixabay.com
So, before I start a cold war here, between people using Rails (Ruby) and PHP, let me tell you that I am not here to debate whether which language is better. For me, or for any experienced programmer, either would be the same. Its just a matter of likeliness as to who prefers what and what is easy for them.
In this blog I would mostly be discussing about the importance of both of them and how they differ from each other. So, if you are new to Rails and PHP both, then this could be the perfect blog for you, if you want to select one from either of the two. So lets get started. Shall we?
Some Basic Background
To start with, PHP is a scripting language, whereas RAILS is a web development framework, which is based on the scripting language Ruby. PHP is a widely used programming language for websites like Facebook, WordPress, Yahoo, Flickr and even more. PHP is extremely fast, n times more stable than Rails and even has a bigger community of developers to support it.
Rails is fully based on Ruby. It is extremely easy to use and get started with. Environment in Ruby is very automated. Ruby is really quite an amazing language. Unlike PHP, it really is Object Oriented from the ground up. Its code is very concise and powerful. Gems (extensions) enable you to bolt on needed functionality. After coding in Ruby, I find coding in PHP rather tedious.
The Good, The Bad and the Ugly
My advice is PHP, because using PHP on the basic level is very easy, there’s a lot of people there who know to copy/paste some template code, modify the configuration files and they even might call them selves PHP programmers, that gives PHP a very bad name which I don’t think it deserves.
For a true programmer, it doesn’t really matter what language he uses, it’s what he codes and the way he codes that matters. After learning few programming languages you understand most of them are actually very similar, it’s usually the syntax that’s different (especially in their core mechanism, even for different programming paradigm).
First things you should learn is writing clean and readable code and not to write too sophisticated code, since it’s harder to debug and confusing for someone who don’t know the special tricks of the language (with PHP, you can do all kind of tricky stuff, not all are obvious to other programmers).
Compared to PHP, Rails is also unfriendly when it comes to errors. With PHP, it will spit out errors at you in development and the error messages actually make sense. Typically a page will render but the part with the error will show you, which line the error occurred and the message is useful. In Rails, typically the whole app blows up.
I’m sorry to offend some people here, but Ruby is simply not as straightforward as PHP to learn. It is by all intents an extremely powerful language. I choose to use Ruby simply because as a developer I feel it is a much better language than PHP. But from a learning perspective, it is not. Ruby has many features that are simply not straightforward for a beginner programmer to understand. One such concept are blocks, procs and lambdas, which Rails uses heavily.
The classic Ruby on Rails example I will use is for creating a form:
<%=f.label :first_name %>
<%=f.text_field :first_name %>
Ruby is a dynamic, imperative object-oriented programming. It is dynamically typed, like in PHP, so you don’t need to worry about having to declare variables. Rails is open sourced, works on multiple platforms and can be embedded into Hypertext Markup Language. It is a very high level language. It even offers encapsulation of data methods within objects.
Ruby has super advanced string and text manipulation techniques can easily be connected to DB2, MySQL, Oracle, and Sybase. Big programs written in Ruby are easily maintainable. It has clean and easy syntax that allows new developers to learn Ruby very quickly and easily. It not only has the ability to write multi-threaded applications with a simple API but also offers advanced array class and the ability to write external libraries in Ruby or C.
Ruby Allows “reserved word” to be used as an identifier as long as the parser doesn’t perceive an ambiguity. When compared to PHP, Ruby has many security features and powerful string handling.
So, the question of the decade is… With all of these features, does it make Ruby a better perspective when compared to PHP?
Unfortunately, it is not so black and white, and many variables come into play when determining if PHP or ruby on rails should be used to develop with.
For instance, Ruby on Rails is a much more complex language to setup a development environment in. Consequently, the tacit knowledge required for Ruby immediately jacks up the price in the programming market compared to PHP development. A PHP developer on the other hand can simply use a conventional package such as WAMP, MAMP to get their dev environment setup in less than 5 minutes.
When I started coding in Ruby, Gems confused me more than they helped because there was too much ‘magic. Once I learned that you could (and should) just read the source code for gems, everything made so much more sense. Due to the pluggable nature of gems and the community’s standards, gems can give your application a tremendous amount of functionality very quickly.
Some gems that I cannot live without: Devise (authentication – handles user logins, social sign on, forget password work flows and so much more),Paperclip (file uploads – even handles uploading to S3, image cropping/re-sampling), Simple Form makes forms incredibly simple to standardize and render on websites.
PHP was designed as a hypertext pre-processor, which means that it executes only when there is a web request. Compared with Ruby, which runs a process. In Rails, you can easily set up background jobs using Sidekiq or Resque. This also adds to Rail’s ability to scale easily. In our applications, we move a lot of stuff that can slow down requests like emailing users into background jobs.
Now, PHP can do background jobs using Gearman but that’s not standardized – you have to install the PECL extension. In Ruby/Rails, background jobs are a non-issue. You just do it.
The Tug of War
Now that you have read about a lot about PHP and Rails, let have a comparison look at them. Let’s check which one is at par in terms of using resources and even in terms of the performance (speed).
Resource Usage and Speed
In terms of memory usage it’s generally going to be Python > Ruby > PHP, which of course leads to Django > Rails > PHP. Not just memory but that also tends to hold for raw ruby on rails vs php performance. Also something worth noting here is that, of course, there are no absolutes here. There are plenty of usage scenarios in which Ruby will beat Python, hands down. I think we can all agree that Ruby and Python will always beat PHP, though.
My own experience is that Rails memory usage can be high, especially on 64 bit machines (minimum is around 95-100 MB with as thin as web front-end). PHP tends to be used with different patterns so it is a bit difficult to compare directly.
That being said, it’s still very easy to make a crappy, slow, and inefficient Django application and a lean, fast, and efficient Rails application, or vice-versa. Skill, knowledge, and expertise with the system you are using will do far more for its memory and performance footprint than just the framework itself.
Database optimizations, server choices and architectures (Apache vs. proxy setups using nginx/lighttpd, etc.), and fundamental design decisions are likely going to overwhelm the framework’s inherent characteristics pretty quickly.
If you run typical benchmarks between Ruby and other languages, Ruby loses. Ruby probably wouldn’t serve you well in writing a real-time digital signal processing application, or any kind of real-time control system. Ruby (with today’s VMs) would probably choke on a resource-constrained computer such as smartphones.
Remember that a lot of the processing on your web applications is actually done by software developed in C. e.g. Apache, Thin, Nginx, SQLite, MySQL, PostgreSQL, many parsing libraries, RMagick, TCP/IP, etc. are C programs used by Ruby. Ruby provides the glue and the business logic.
The Question is “WHY PHP then?”
Now lets talk about PHP. PHP runs extremely slow on apache server. Even if you try to run a PHP page even without any script, just an empty php page, it will still takes like 10 times more time to load compare to JSPs or Java. But again, the question of a million century is that if that is so, then why haven’t Facebook ditched PHP yet? The reason Facebook hasn’t migrated away from PHP is because Facebook’s engineers have managed to work around many of its flaws through a combination of patches at all levels of the stack and excellent internal discipline via code convention and style.
The worst attributes of the language are avoided and coding style is rigidly enforced through a fairly tight culture of code review (failing to adhere to the style and “going cowboy” by writing sloppy code results in pitiless mockery by one’s peers). Engineering management has never had to take a strong hand here; this arose largely due to key internal technical leaders just sort of corralling everyone else along.
And Facebook doesn’t just use PHP of course. It also includes C++ as its core. So, for PHP use some kind of opcode cache like APC or eAccelerator otherwise PHP has to parse your files on each request. For general apache tuning you should do some googling, couple of things like disabling .htaccess files comes to mind but it still should be faster than JSP.
So in the end I guess what I’m saying is if you your way through Rails, than you should stick through Rails as long as you are not planning to start a whole new project based on PHP and start a company along with it.
Here are some articles that will help you to get more detail about the Rails vs PHP so just go through the link.
- Amazing Guide on Learn Rails development
- Ruby vs Ruby On Rails- Which one is Better?
- Ruby vs PHP-Which Technology Is Best?
- Top 10 Most Awesome PHP Interview Questions for Experienced