Back to PHP after 2 years on Rails

In this post Sivers writes I spent two years trying to make Rails do something it wasn’t meant to do, then realized my old abandoned language (PHP, in my case) would do just fine if approached with my new Rails-gained wisdom.

“Is there anything Rails can do, that PHP CAN’T do?”

The answer is no.

I threw away 2 years of Rails code, and opened a new empty Subversion respository.

Then in a mere TWO MONTHS, by myself, not even telling anyone I was doing this, using nothing but vi, and no frameworks, I rewrote CD Baby from scratch in PHP. Done! Launched! And it works amazingly well.

It’s the most beautiful PHP I’ve ever written, all wonderfully MVC and DRY, and and I owe it all to Rails.

Read the original post by Derek Sivers

PHP Meetup Required

Well long back once, I tried for a php meetup, some where in technopark trivandrum, and failed miserably. Well this time I am not going to announce anything like that. Those of you who see this, give a good boostup, and send feedbacks.

Once we have a handful of interested people, we will think of the venue as well as the date. Though it would be better to have either on friday or saturday evening, as that would affect the company which we are working for least.

Calculations ? Try and use SQL

Hmm.. yes.. I literally meant it.. for several things you can directly use SQL calculations, though the queries and functions shown here are tested only with mysql, it should work with most other sql servers too. The cliche is if you have the sql server on the same server where your webserver and php interpreter is running. In simple terms, if you connect to mysql on www.jijutm.com, sql can be used to a great extend to do calculations.

Some time back I did read on some other site, where two dates where calculated for the current month, using php mktime and date functions, to be passed in an sql query, for a range selection. I am not for an argument, but it seems more efficient to use the date calculations directly in the sql query, than pre calculating this unless otherwise it is needed to be printed in the output. Even then, the calculation can be done by the sql, and the result returned.

SQL calculations along with SQL session variables, will help a programmer achieve a very high optimization level for the scripts, thereby reducing webserver, and interpreter load.

Date calculations can use date_format, date_sub, unix_timestamp and from_unixtime and other related functions. Aggregates has all complimentary functions like sum, abs and others. By using a combination of these, any calculation can be achieved by sending an sql query.

Quotes – All about variable evaluation

The doubt on whether to use double or single quotes for static strings,  has been a heated discussion on many a discussion groups. As of late I did a small bench mark, and has found that the encapsulation of variables in double quoted string is a slight higher load than concated string, even when the static string is quoted in double, whereas the most efficient is to specify single quotes for quoting static string.

Continue reading “Quotes – All about variable evaluation”

PHP Mime Mail Class

There are a lot out there; but my favourite is been PHPMailer

I was always on the look for a decent php mail class with smtp auth and mime mail support.  Then one day stumbled on this sourceforge project. There it was with other features too which I wished to have

  1. Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs,
  2. Redundant SMTP servers, Multipart/alternative emails for mail clients that do not read HTML email,
  3. Support for 8bit, base64, binary, and quoted-printable encoding,
  4. Uses the same methods as the very popular AspEmail active server (COM) component,
  5. SMTP authentication,
  6. Word wrap,
  7. Address reset functions,
  8. HTML email,
  9. Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc,
  10. Works on any platform,
  11. Flexible debugging,
  12. Custom mail headers,
  13. Multiple fs, string, and binary attachments (those from database, string, etc),
  14. Embedded image support

Month of PHP Bugs started

The Hardened-PHP Project is proud to announce the beginning of the Month of PHP Bugs. This initiative is an effort to improve the security of PHP. However we will not concentrate on problems in the PHP language that might result in insecure PHP applications, but on security vulnerabilities in the PHP core. During March 2007 old and new security vulnerabilities in the Zend Engine, the PHP core and the PHP extensions will be disclosed on a day by day basis. We will also point out necessary changes in the current vulnerability managment process used by the PHP Security Response Team.

Borland’s RAD for PHP; to be or not to be

Borland’s wholly-owned spinoff CodeGear is releasing Delphi for PHP, claimed to be the first RAD visual PHP development environment. PHP 5.0 is required. The product is jointly developed by CodeGear and qadram software and is essentially the first production version of qadram’s qstudio, which never made it past pre-beta.

The IDE is a Delphi lookalike, built with Delphi but distinct from CodeGear’s BDS (Borland Developer Studio). The main point of interest is the component library called VCL (Visual Component Library) for PHP, formerly called WCL (Web Component Library), which is a set of PHP components modelled after Dephi’s VCL, supporting drag-and-drop form editing and customization with a property editor. Existing PHP libraries can easily be wrapped as new VCL for PHP components.

Read the whole story »

12 PHP optimization tips

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. Avoid magic like __get, __set, __autoload
  3. require_once() is expensive
  4. Use full paths in includes and requires, less time spent on resolving the OS paths.
  5. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
  6. See if you can use strncasecmp, strpbrk and stripos instead of regex
  7. preg_replace is faster than str_replace, but strtr is faster than preg_replace by a factor of 4
  8. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
  9. Error suppression with @ is very slow.
  10. $row[‘id’] is 7 times faster than $row[id]
  11. Error messages are expensive
  12. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.

As blogged by alexmoskalyuk

Hit the WDDX Bug twice

In Saturn we had a handful of XUL projects where the XUL part was for backend administration. It was in the year 2004, when we developed most of the packages, and we were on php 4.2. There was heavy use of WDDX serialization, since that was found to be easy, we never knew about soap and soap implementations. By the end of 2005, most of the projects got woundup, and our XUL first hand developer had also quit. Since then we were swaying away from XUL development, and by mid 2006, we had almost dropped any further XUL support, as well as development.

Recently, the management decided to revamp, and pull out one of the old project to be reworked as a new product with solid backend. It was then I got bitten by the WDDX bug, [#38839], and I overcame that by patching the latest hourly patch.

Later on our COO needed the same project to be deployed on his laptop, where we downloaded the TSW which is Easy, modular and flexible WAMP bundling Apache2/SSL, MySQL4, PHP4, Perl5.8/ASP, Python2.3, Tomcat5, FirebirdDB, FileZilla, Mail/News-Server, phpMyAdmin, Awstats, WordPress, etc. It also includes a web-GUI to control/manipulate all bundled services. But even with the php 4.3.4, it also seemed to have the WDDX bug, but in a different way. ie; when the XUL application sends an AJAX request, where the output was expected as a wddx serialized string, the Apache server started crashing.

Finally in the TSW also, I downloaded a cvs snapshot and patched that, then the error went away.

User input in php command line

Ever wondered how to capture the user input when writing php command line scripts ?

<?php
 
function getInput($msg){
  
fwrite(STDOUT"$msg: ");
  
$varin trim(fgets(STDIN));
  return 
$varin;
}
 
?>

The function above is being used by me in certain command line scripts, where I need user responses.