PHP vs Rails
April 4th, 2008
I was milling over some old PHP code today, funny, because my friend Norm linked me to David Hansson’s blog post…it looks like PHP is a Friday thing!
After looking at my old code, I thought to myself, hmm, what better time to showcase the beauty of Rails. I made a post on a message board I frequent, and there was quite a bit of discussion. Anyways, take a look at this!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | header("Content-type: text/html; charset=utf-8"); include_once('inc.functions.php'); require('inc.drawrating.php'); $uid = $_GET['uid']; $conn= mysql_connect("localhost","db","dbpassword") or die (mysql_error()); mysql_select_db(ave_development) or die(mysql_error()); $q = "SELECT views FROM stores WHERE uid=$uid"; $result = mysql_query($q,$conn); $tmp = mysql_fetch_object($result); $views = $tmp->views; $views++; $lview = date("Y-m-d H:i:s"); $q = "UPDATE stores SET views=$views, last_view='$lview' WHERE uid=$uid"; mysql_query($q, $conn); $q = "select * from stores where uid = $uid"; $result = mysql_query($q,$conn); $row = mysql_fetch_object($result); $name = $row->name; $phone = $row->phone; if ($phone == 0) { $phone = ""; } else { $phone = " - " . format_phone($phone); } $description = $row->description; $description_lb = str_replace("\n", "<br>", $row->description); $address = $row->address1; |
This is the code I would use if I were to do the same thing in Rails.
1 2 3 4 5 | # the controller class StoresController < ApplicationController @store = Store.find_by_id(params[:id]) @store.increment!("views") end |
1 2 3 | # I'd place this in my 'show' view <%= number_to_phone(@store.phone) if @store.phone.exists? -%> <%= simple_format(@store.description) -%> |

April 10th, 2008 at 5:27 pm
While I much prefer Ruby to PHP and have switched full time to Rails development, so kind of agree with what you are saying, you can’t really compare a language with a framework! If you also included all the code behind each of those (ie, quite a lot of rails code, and whatever is in your PHP includes) I’m sure PHP would come off looking a little better than it does here. And that PHP could be tidied up a lot anyway.
All my old PHP code is the same though, I wonder how much nicer it would be if I went back to coding PHP with what I have learnt with rails…
April 10th, 2008 at 9:56 pm
As for the PHP code that I posted, obviously that code could be drastically reduced with SQL triggers and better code, but for the most part, a new programmer using PHP vs one using Ruby, Ruby is going to be the winner
April 11th, 2008 at 1:31 am
I must agree with Mike. You cannot compare a scriping language with a Framwork. I recently saw some board message were someone asked if Ruby on Rails is better than using TYPO3 for a Web Application. TYPO3 is a powerful and complex CMS based on PHP. Same condition as with your posting applied here, you cannot compare apples and oranges.
However, what you surely can compare is the great readable syntax in Ruby and the good community in rails, their overall willigness in picking up the latest technology (best practises, patterns, git and a like) and their ability to learn and adapt frequently.
From my PHP-Years i can tell you that the majority of PHP-Coders never adapted to new technical expertise while i can see from the core rails community, that this makes up 50% of the fun - learning and improving that is.
-act
April 11th, 2008 at 8:27 am
Well done. This is a fantastic representation of power, flexibility, and ease of use with Rails.
April 16th, 2008 at 11:53 am
Well, maybe you can’t compare a language to a framework, but one obvious point of note is that the Rails code is properly segmented into pieces, a controller piece and a view piece. The PHP code is all in one entity. I know PHP templating frameworks exist, but it seems PHPer’s have never heard of them or just don’t like them. (I’ve actually heard speak disdainfully of the whole MVC concept in general, voicing the loud arrogant sentiment of “real coders don’t etc. etc. etc.”) They actually WANT to put controller and view in the same component and they actually imagine that’s a GOOD thing! Then when a page layout needs a minor tweak, rather than having a construct a designer might actually be able to modify themselves, they–the PHP developers–have to go in and make the change. It’s kind of sad that so much of the code driving the Web is written in this backwards language by people with backwards attitudes. “Want to change a template in a PHP blogging system?” Oops, you have to *learn* PHP to do so. “What if you’re just an average user and not a programmer?” Well, tough, everyone should learn PHP just like I did. This attitude should have been killed off by 1999, but sadly it’s still prevalent. PHP is a solid language, but the way it’s used in web applications is all too often just plain atrocious. Rails and similar frameworks give us hope that things can be better designed and organized down the road, but with all the legacy PHP cruft standing in the way, that’s going to be hard. (Of course, PHP is still miles better than any variety of ASP.)
April 21st, 2008 at 5:06 am
Sviergn,
I am a 6-year veteran PHP coder who is finally making the jump to learn RoR.
I had built my own frameworks that cover much of the functionality of MVC methodology–
I think the major concern with PHP is that many of the PHP programmers out there are merely dabblers, as the language requires little skill to get going. I find that to be one of its best points, and I recall that is why I picked it up so well so quickly back in the day.
Regarding PHP frameworks, have you seen Symfony?
May 25th, 2008 at 9:50 pm
Why would I need Rails when CakePHP is the same thing as Rails but its no the whole PHP language itself?