Genotrance

Icon

Random thoughts, ideas and experiences

PHP is Easy

Among Perl, PHP and Python, I have the most experience with PHP. Considering my fascination for web-based applications, it was a very appealing replacement for the C based web apps that I was writing. My background in C led me to write everything in C, regardless of whether it was appropriate or not. PHP was very easy to pick up because of its C like syntax and API. I’ve not written much C after that – well, for web apps at least.

Getting Started

It’s easy to get started with PHP. On Windows, I just install XAMPP and I’m set. It can’t get any easier. On Linux, you can select the “Web Server” component at install time or use the package manager of your choice to install PHP with Apache. XAMPP also has a Linux version if you want to go that route. PHP also supports IIS and a bunch of other web servers if you have preferences there, though it might be a little tedious to get things up and running. If you’d rather use a web hosting service, you’d surely be hard pressed to find one that didn’t support PHP.

Born for the Web

In my experience, web apps are so easy to write in PHP that it is hard to imagine doing anything else with it. I can’t imagine writing console scripts or GUI apps in PHP; in my mind, they just don’t gel. There are some GUI bindings for PHP such as PHP-GTK but I haven’t bothered to check them out. That PHP is an excellent web programming language is attested by the fact that there are several powerful web apps written in PHP. The list is endless so it’s better not to start one. To put it in one sentence, PHP put the ‘P’ in LAMP (Linux, Apache, MySQL, and PHP).

Too Easy, Too Difficult

Yes, PHP is easy. But it is so easy that there are too many poorly designed PHP web apps out there diluting the pool. A quick search on Freshmeat or SourceForge will result in an endless list of these applications whipped out by Joe Coder. Some folks don’t take PHP too seriously because of such “scratch an itch” projects that make PHP look anything but enterprise ready. In my opinion, PHP is enterprise ready. But that’s a completely different story.

It is very easy to create simple programs that do specific things in PHP. Say you want a simple web form processor that dumps the data collected into a database. Do it in PHP. It is quick and effective. There’s no point in whipping out JSPs for this task. There’s no point using a machine gun to swat a fly. However, when a program outgrows its original intention, it can quickly go out of control in PHP. I’ve seen my own programs become ugly over time. One line here, one file there, one function here and suddenly your simple form processor has become a portal. I’ll take the blame for poor planning on my part without question. Poor planning will cause problems in any programming language out there. But there’s something about PHP that makes it hard to write sensible, clean and consistent code, something that makes you want to whip out code without thinking; it is so easy to do that in PHP. It would suffice to say that it is easy to write ugly code in PHP. If you can follow sound programming practices then you have a very powerful tool available to you in PHP. But if you are not careful, you will want to rewrite your application in a year. It has happened to me. It may happen to you too.

Don’t get me wrong. I am a big fan of PHP. It is a very powerful and flexible programming language in which you can write just about anything. Just be careful with the language. It is easy to get carried away and PHP won’t stop you.

Best Documentation

I’ve grown quite addicted to the excellent API documentation for PHP. It is one of the reasons why it is so easy to program in PHP. In my opinion, it is the best documentation I’ve come across so far. It is so much easier and obvious. Python’s API is dry and boring with almost no examples. And where on earth do you start with the Ruby on Rails API?

I love the fact that people can add comments to the PHP API. Those comments and code snippets have saved many hours of my time. Having relevant comments right there is a simple yet a powerful concept. The flip side is that you can’t do much in PHP without this documentation. The API is so extensive and inconsistent that you have to check it for every array_push() and substr() you do. That might be the reason why I’m finding it so hard to get over it; I’ve used it so much that I don’t know any better.

The API can be browsed and searched online and is also downloadable in a variety of formats. There are also countless online tutorials to help the newbie. And with Google open in a browser, all your PHP needs are at your fingertips.

Conclusion

PHP is the way to go if you have to put together any kind of web based application provided you take adequate precautions to be consistent and clean. It is simple and yet powerful and has proven itself to be very useful to me time and again. I endorse it whole heartedly and recommend all developers to learn it and keep it in their toolkit.

Advertisements

Filed under: Programming

6 Responses

  1. […] There is a lot of attention being given to PHP, Perl and Python on this blog. Where’s Ruby? And where’s Ruby on Rails? Over the few past years, I’ve realized the importance of having the right tools for the job. That’s why I use Perl for scripting and PHP for the web. It’s not like I’ve not given Ruby and Rails a try. In todays market, you can’t afford to be stuck with the wrong tools. You have to keep up with the times in order to succeed. I’ve kept my eyes open ever since. Regardless, there has been too much activity on the web surrounding the whole Ruby on Rails phenomenon for me to not have noticed anyway. […]

  2. Rob Ristroph says:

    I agree with your sentiments. However, I would not give up on C web programming.

    I first used mshare precisely because it was a C webapp, and I felt it would be easier to extend and modify if I needed to, and fast.

    I write scripts in perl and php just like everyone else . . . in fact, counter to your remark, I do command line scripts from PHP all the time. If there is already a bunch of PHP code in the web interface that interacts with the database and etc, and you want to automate some administration tasks, it is easy to re-use that code and make a php script to be run from cron or the command line. ( I avoid putting those kinds of things on the web having the cronjob call it with “wget” or something, because there are fewer security issues to deal with. )

    But, in the long run you can’t beat C for speed and low resource use. All those neat things in perl and php that make life so much easier, such as the string manipulation stuff, have the equivalent in a C library (you just have to find which one and how to use it, often not a trivial task). I think people don’t pay enough attention to speed and resource use. Fast light code makes it easier to scale and pack more sites on one machine and saves on energy costs.

    If something is going to be written once and used only a few times, like a special admin script, write in whatever is easiest to write it in. But if something is going to be written once and then run thousands of times a day for years, then take a little time and care and write it right the first time.

  3. francisco says:

    can someone recommend me a good free php ide or editor that has code completion and api documentation integration?

  4. There are other alternatives to a WAMP Server. See what works the best for you…

    I recommend the Web-Developer Server Suite.

  5. protospike says:

    Nice article 🙂

    I agree that it’s great that people can add code snippets in the PHP API documentation. A problem is that there are a lot of code snippets that are misleading and confusing (heh, I believe I have contributed crappy code snippets myself!)

  6. eksith says:

    Anything that’s easy to use will get abused with poor quality work.
    That’s just the way it is, sadly.

    I’m not a PHP programmer, but I’ve done a few sites with it, and it’s incredibly easy to get carried away with bad code. But I’ve learned 😉

    There are also frameworks like CakePHP that make rolling out a web site very easy.

    Another language to consider is Python.
    Using Django (a framework like CakePHP, but for Python) actually encourages new programmers to write applications with more quality. You can run it with an Apache module, mod_python. The same way as mod_php.

    Both languages are more than ready for the web, but like all applications, it’s the programmer’s responsibility to write quality code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: