While trying to pick a consulting company to work with, people occasionally ask us about the differences between PHP and Ruby on Rails. Here are my thoughts on the matter. Please feel free to add to this list in the comments.
- PHP is a programming language, whereas Rails is a framework for writing web applications using the Ruby programming language. So right off the bat this is not an apples-to-apples comparison.
- There are a lot of PHP web application frameworks. Some are good and some are not (for some definition of the word good.) Many frameworks (including Java and .NET based frameworks) have borrowed heavily from Rails.
- Ruby is a very succinct language. This means that you can be very expressive and do a lot in very few lines of code. PHP is very well known, so it might be easier for many people not familiar with Ruby to stay with PHP.
- PHP programs tend to require less memory to run so they tend to be easier to deploy. With the release of mod_rails and Ruby Enterprise Edition Rails becomes easier to deploy than PHP once the initial Apache add-ons are made.
- PHP programs require less memory because by default they don’t have the overhead of a web framework. Although I haven’t seen any stats on this, I would imagine that a PHP app running inside a web framework as robust as Rails would have similar memory requirements.
- The additional add-ons only have to be made once per machine and are super easy to do.
- Rails is easier instead of just as easy because Rails has migrations which make deployment of database changes easier than any open or commercial solution I have ever seen.
- They are both equally easy or difficult to scale. The first level of scaling is just adding more memory/cpu/server instances. When scaling thusly they are both just as easy. Once this level of scaling is exhausted, they are both equally hard requiring all of the same methods as scaling any other web application on the internet: Code optimizations, caching, more servers, database scaling, content delivery networks and more.
- Rails is cooler. Sorry, but it’s true. Don’t worry though, eventually there will be something even cooler than Rails and then I’ll write an article comparing that to PHP or Rails.
- I’m sure there are exceptions to this, but in general the results of using Rails (final product) will be heads and shoulders (probably even arms and legs) above the results by someone who wrote their own web application framework.
- The following would be true of any MVC framework (like Rails) worth anything, regardless of language:
- Code is well encapsulated.
- Anyone familiar with the framework should be able to pick up where someone else left off fairly easily.
- Standards and best practices are well defined.
- One should be able to do a lot with very few lines of code.
- Things Rails does for you that seem to be specific to Rails (I would assume that somewhere there is another web application framework that does some or all of these things, but I am not aware of one).
- Super easy to write a lot of code/test/functionality in record time.
- Database versioning.
- REST/crud is very deeply ingrained.
- Lots and lots of helper methods.
- Significant JavaScript/AJAX support.
- Very easy to extend.
- Built in testing.
- Vibrant community.
- Lots of plugins.
- Lots of blogs/tips/tutorials.
- Lots of conferences/friends.
- Very well tested.
- Feels like it was written just for me (most of the time).
- Easy for a designer to learn.
- Because so much is done for me by the framework, I can spend more time working and thinking about the users and what’s best for them instead of things like rewriting my database connectivity stack.
My advice
Picking one over the other has more to do with who you trust to build your application for you and what they like. Or if you are a developer, find out which one best fits your tastes and temperament. Neither will save you from bad code or bad decisions or misunderstanding but for me Rails feels like home. Find something that feels like home and then go write something that users will love.
Here are a few things we’ve written using Rails: Less Accounting, More Honey (time tracking), Less Projects (project task tracking), Less Memories (personal memory management), Lovd by Less (open source social network)
Three more points, off the top of my head:
1. Rails has, at this point, a better rep. You could say that it doesn’t matter, and ultimately you would be right, but it is something you want to take into account. Being a “Rails shop” will give you a slightly different position, in your customer’s eyes, as opposed to being a PHP-based company. Maybe I should have said that PHP has a bad reputation?
2. Ruby is much better than PHP. No framework, regardless of the quality of its implementation will make PHP as fun to code in as Ruby is, or as powerful. PHP does not have blocks, pure object-orientation, metaprogramming facilities (or maybe it does). It doesn’t facilitate the creation of domain specific languages. PHP does not have Ruby’s outstanding builtin library. PHP does not have (to my knowledge) an equivalent for some libraries and utilities such as hpricot, chronic rspec or rake. Does PHP have anything as powerful as Capistrano when it comes to deployment?
3. PHP is a programming language for the web, Ruby is a general language that is well suited for writing web applications. That means, if you ever need to write a small GUI application or some esoteric thing, you can probably find a way to do it in Ruby. If you don’t like Rails itself, there are lots of other frameworks that you can use: Merb, Sinatra, Markaby, Ramaze, Camping and more.
4. (Bonus): You won’t see people headbutting in the PHP community, except when the projects are doing really really bad.
You can write non-web apps (e.g. desktop apps) in PHP, if you really want to. (I don’t remember how… I just remember seeing something that makes it possible.)
PHP has a bazillion functions that can do all sorts of things, and the documentation is really good. The language, however, evolved out of primordial soup by a series of ad hoc additions, and your code will look as messy as you would expect given this source. Ruby, on the other hand, was created by an intelligent designer, and as a result is fairly consistent and makes a lot of sense. Use it and you’ll see that it is very good.
Great post! It was very informative. As a ex-php developer, current asp.net developer, I’m always curious about ROR.
@Eugen:
Great points. Regarding the deployment issue though, php does have something as powerful as Capistrano. It’s Capistrano.
@Brandon:
There are a few frameworks that allow you to write cross platform desktop applications using Ruby. Among others why created Shoes and Charles created JRuby.
@Ben:
Thanks! If you’re ready for a life changing experience, download Rails.
-steve
As a PHP developer, I’ve been asked the same thing a few times… my response usually goes along these lines, depending on who I’m talking to:
1. For Businesspeople/potential clients (who ultimately don’t care about implementation language but just want the best solution at a good price, including ongoing maintenance costs):
2. Talking to other developers/web geeks:
I like PHP, it’s a swiss army knife for web development. There’s nothing web-related it’s not capable of, although you have to have discipline and sound compsci fundamentals to produce quality software with it.
I’ll be continuing to keep my eye on RoR, may even do a few internal projects with it, but I can’t see myself using it for commercial projects for a good while yet, especially while it’s someone else’s money (and my reputation) on the line.
Ruby is a good language and Rails is a good framework.
But Rails is only one of approaches to web development in Ruby by combining MVC and Active Records. Rails does not fit into every web projects even the trivial ones. It fits to some, not all. What Rails is if I don’t want to use Active Records because my database is very complex compared to simple cases that Rails is proved to cover? What I need to do when I discover that the way that MVC is implemented in Rails is not strong enough for view extensive web applications. Basically, Rails implements MVC push, which is that same to Java Struts, Python’s Django and plethora of PHP frameworks: Zend Framework, SolarPHP, Symfony, CakePHP. MVC Push is good and classical. The View part in MVC Push like Rails is too stupid, passive and basically is nothing more than a template renderer. Why View can not be smarter and is designed to be shared between Controllers or Modules. Rails Components is a workaround to it but it is slow and unnatural.
What Rails recommends and tries to enforce is to hide database from developers and expose it as a collections of objects. It is good and bad at the same time because this approach can not cover every use cases. What Rails Active Records does is to make basic operations on relational database easier. Period
http://www.oreillynet.com/ruby/blog/2007/09/7_reasons_i_switched_back_to_p_1.html: 7 reasons I switched back to PHP after 2 years on Rails
http://davidkellogg.com/blog/2007/10/02/why-i-switched-to-rails-and-back-to-php-in-one-day/ Why I switched to Rails and back to PHP in one day
Good developers never force Rails to what can be easily done in raw PHP or PHP frameworks. Rails is not perfect so please do not protect it from unintended uses.
First of all, I don’t think the title of this post is really appropriate. It’s like comparing apples to oranges. Maybe it should have been titled, “Comparing PHP to Ruby” or “Comparing PHP/Zend (or CodeIgniter/CakePHP) to Ruby on Rails” I think you’re better off comparing Python to Ruby instead.
I like to think I’m fairly language agnostic. It’s pointless to argue which language is better than the other and that’s the only argument this sort of post seems to be promoting. Every language has a time and place for itself. Some languages are better suited for certain environments.
But, while everyone is band wagoning, why don’t we discuss Ruby on Rails ability to scale compared to PHP’s? And why Twitter looks like it will be abandoning RoR.
Yay Rails! I built both seocracy.com and datapresser.com on Rails.
I will definitely do more rails development in the future. The only thing i hate about ruby ois its Threading. Bleh.
I figured I’d let the firestorm occur before saying anything..
My view of PHP is seems like it was written by a mediocre C programmer who wanted to do some simple things with no intention of doing structured programming. There is only one data structure – arrays, which act as both lists and hashes – and, at least until objects were added, no real ability to add new ones. The C-style syntax is very restrictive. It looks fine for the simple case, but tends to get in the way for anything even slightly complicated, like defining a tree or nesting functions. Ruby has its problems, but there are pretty good reasons for most of the stuff and usually it works very well. I guess people tend to go with C syntax because it’s what they know, or what has the largest market share. But when you use a language with syntax catered to what it can do, like Ruby or Haskell, you realize the clumsiness of C pretty quick. I know this is all nothing new and I’m sure you agree, but still I think I’m coming from it from a different, more pessimistic, perspective.
On to the framework itself, the whole MVC thing is without any doubt tons better than procedural style. So because Rails is based on a pretty good pattern and built with a well designed language, it’s pretty good. But I think it also tends to not be very cohesiveness, or well designed even. Coming to mind are that controller methods and template helpers call an action (or not) or modify the template directly, respectively, rather than just returning a value. Merb at least gets those right. I’m not sure how it compares to something like CakePHP since I don’t have first hand experience with Cake, but my guess would be that Cake is better, except for the language you’re forced to use.
“Rails is cooler. Sorry, but it’s true. Don’t worry though, eventually there will be something even cooler than Rails and then I’ll write an article comparing that to PHP or Rails.” Have you tried continuation-based servers? They certainly have their problems too. (Lets start with having to use Smalltalk, Lisp or Perl.) But they’re better to write in generally.
Here’s some things I really don’t agree with! -Database versioning.
I actually like just having some SQL scripts instead. I’ve had enough times where I needed to change data in a more complicated way than would be allowed by ActiveRecord. Using multiple languages isn’t as bad a thing as the Rails community seems to believe.
-REST/crud is very deeply ingrained.
REST, at least, is a fairly new thing in the Rails community. The MVC stuff does work very well for it, but I think it could be smoother. It’s certainly very easy to break with the RESTful style. Also, following REST to its fullest extent, you would entirely eliminate the controller layer. You could get rid of the view as well, if you wanted. Rails’ isn’t really up to the task out of the box right now, but that could be changed..
-Very easy to extend.
I think it’s actually difficult to extend, but everything is disagree, it could be easier and the ease there is isn’t due to it being designed to be extensible.
-Feels like it was written just for me (most of the time).
Well, I can hardly disagree with your feelings, but at least it wasn’t written for me. :p
-Because so much is done for me by the framework, I can spend more time working and thinking about the users and what’s best for them instead of things like rewriting my database connectivity stack.
Actually one of the main things I like about MVC is how little it tries to do for me. I like things to get out of my way and let me do my own thing. Compare to ASP.NET. I’m not talking about the ORM though. I’m perfectly happy to let some code generate queries for me. But I use an ORM or equivalent on every project, regardless of platform or language.
Something weird happened with one sentence..
but everything is disagree
becomes
but everything is easy to change in Ruby. So I don’t exactly disagree
So yeah, didn’t mean to write a whole damn page. XD
PHP doesn’t have blocks <3>