PHP vs Rails

April 4, 2008

 
No Gravatar

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

  • chriswpage
    I’m editing an article on this topic titled “Choosing the best tool for the Job: CMS and SaaS, PHP MVC, Django, or Rails”. I’ve gone a bit more in depth with pros and cons considering:

    -Budget and Overhead Factors
    -Work force skill level
    -Stability of Framework/Language

    It's still a work in progress, though give it a read at:

    http://www.chriswpage.com/2009/05/choosing-the-...
  • hariskotoul
    I work with both ROR and Cake. For me there is no option but ROR. For me
    - Rails is clear, readable and fast
    - cake is cryptic, obscure and slow
    By the way look at the literature and the programmers base. I mean if you are an experienced Cake programmer you probably have a ROR background. But then why move to Cake. I did it just to add another buzzword on my CV
  • Derek
    This is a really terrible comparison. You show a from start-to-finish PHP example, but only show the by product of the time spent setting up a rails project. The only thing that comes to mind is you are an idiot.
  • That went way over your head.
  • Darren
    Why would I need Rails when CakePHP is the same thing as Rails but its no the whole PHP language itself?
  • >> Darren

    lol, php cake is the worst from all the php frameworks.
    good luck with that!

    cake is refactoring its "ActiveRecord" implementation at a tremendous slow pace
    implementation to match the one on rails so why should you wait and refactor all
    of your code instead of concentrating on your own application code?

    languages ruby, php
    frameworks rails, cakephp

    if you compare ruby and php ruby is a more powerful language
    if you compare rails with cakephp well.. I'll say rails is better.

    ither way rails is not like the "PHP language itself"
    I'll say use rails and then compare them.

    is like when you are used to something and then you do it in another way.
    and you think there is not so different.

    then you go back to the way you use to do it and you feel the difference.
    then you switch back to the new thing and realize that this is how you should
    approach the problem!
  • 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?
  • Sviergn Jiernsen
    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.)
  • Well done. This is a fantastic representation of power, flexibility, and ease of use with Rails.
  • ACTRAiSER
    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
  • Jonathan
    That's true, you can't compare PHP to the Rails framework...but even against cake PHP, Rails is leaps and bounds better!

    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 :)
  • Mike
    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...
blog comments powered by Disqus