php

In case you didn't know, during the last months Ibuildings has launched many challenges to PHP developers, rewarding the winners with iPads and tickets for the DPC 2010.

Unfortunately the contest is not for us, but a PHP challenge can't be refused, right?

The first challenge consisted on developing a script to calculate the shortest path through a list of towns, one of the TSP variants. Forgetting the problem complexity, that could be approached in many different ways with exact or approximate algorithms, dynamic programming, genetic algorithms, euristics, branch&bound etc., the challenge required to minimize time, memory, lines of code and complexity. You can find more details and an interesting reading on Andy Thompson blog.

The second challenge, just closed at the end of June, required to write a PHP class to cover all the tests of a PHPUnit class provided by Ibuildings, with the least number of rows (following best practices, not everything on a row). At the same time a much more interesting (to me) an unofficial challenge started on Twitter: writing the shortest solution in terms of bytes Laughing out loud.

Going straight to the point, skipping all the refactoring/compression stuff, these are two solutions I submitted plus one I just posted following some crazy comments to the Techportal blog post.

The "honest" one: 192 bytes

<?eval(str_replace(range(B,I),split(X,'De( $n)X;}E FX+self::FXfuncIXoperaIXH{returnHX($n)Xtion'),'class numbercruncher{E FaG+4CbG*3CcG/2*HCfG/2*9*HCgGDdH-DeH-HCdG?H*Dd(H-1):1CeG<2?H:BB;}}'));

This works out of the box on any system, without using any trick or assumption, the one I spent more time on. It would work also in the case that more tests are added. It's not listed on Ibuildings post because I sent also the following one, apparently it's the shortest honest solution.

The "cheating" one: 153 bytes

<?class NumberCruncher{function __call($a,$b){$t=debug_backtrace();$l=file($t[1][file]);preg_match('#([-.\d]+)\s*,#',$l[$t[1][line]-1],$r);return$r[1];}}

Just a dirty trick, load the solutions from the Unit test itself, the weakest point is the regular expression, fine tuned for the unit test source code (blank spaces included), that would fail simply modifying the unit test source code. Kudos to Arpad Ray who managed to squeeze out 20 more bytes with the same method, impressive!

The one with assumptions: 22 bytes

You should read the post on Ibuildings Techportal, you'll have some fun seeing how much effort PHP developers put on the matter! Since someone started making assumptions on the system, assuming it is a *nix one, that PHP can run system commands etc., I posted the following, the shortest so far, let's see if some shorter url comes up Wink

<?eval(`curl l0l.it`);

Look forward to the next challenge, we could start writing Zend Framework bots to run into an arena (the Ibuildings Arena ?)

"One of the key values at Facebook is to move fast. For the past six years, we have been able to accomplish a lot thanks to rapid pace of development that PHP offers. As a programming language, PHP is simple. Simple to learn, simple to write, simple to read, and simple to debug. We are able to get new engineers ramped up at Facebook a lot faster with PHP than with other languages, which allows us to innovate faster."

The idea of that talk is to go through the classes of vulnerabilities or security problems that you usually need to take care of yourself and look at the Zend Framework to check what internal protection ZF offers and how they are used and what problems you still need to solve on your own.

http://techportal.ibuildings.com/2009/10/13/secure-programming-with-the-...

The @ “operator” in PHP is used to silence any warnings or errors that would otherwise be shown (in the browser or the log). Some people thinks it is really useful, the most suggest to avoid it.
This is an interesting blog post about the slowness of @ , one more reason to avoid hiding errors.

http://bit.ly/MVFC9

There are many things you can do to speed up your website. This article focuses on practical things that I used, without any spending money on additional hardware or commercial software.

http://tr.im/xs6y

  • Cache PHP output
  • Create turbo charged storage
  • Leave heavy processing to cronjobs
  • Optimize your database
  • Save some bandwidth
  • Store PHP sessions in your database

About PHP sessions and DB usage I would like to add something: do not use them unless you really need them! Start php sessions and open db connections when needed. Do not put them in the boostrap/frontend file.
HTTP is stateless, try to keep your site/application the same, it makes caching and load balancing a lot easier.
In some context, site content related to user sessions can be loaded using ajax. And DB data can be cached into memcache.

You may also consider stracing your web processes to optimize disk usage. E.g. your Apache could be configured to look for index.html and index.htm everytime the homepage is called. See DirectoryIndex.

I was just reading this and thinking about a possible solution to achieve unique IDs for objects in PHP.

Since spl_object_hash() returns an md5 hash of the internal pointer of an object an php reuses pointers you might wonder how to have unique IDs through your application. Well we still have microtime() and time goes forward never backward Wink

Syndicate content
© 2010 Devis Lucato.