PHP in a Tweet
twitter , php , code-golf
Yesterday an ex colleague tweeted something that captured my attention:
#php
function sanitize_http_protocol($link,$_protocol='http'){ return $_protocol.'://'.array_pop(explode('://',$link)); }
— Stefano Azzolini (@lastguest) September 11, 2012
So I started thinking to a Twitter-powered code golfing competition. Looking for other examples you can see these:
class Container { protected $s=array(); function __set($k, $c) { $this->s[$k]=$c; } function __get($k) { return $this->s[$k]($this); }}
— Fabien Potencier (@fabpot) April 3, 2009
This is Twittee: a dependency injection container in a tweet.
require __DIR__.'/c.php';if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; })) throw new Exception('Error');$c();
— Fabien Potencier (@fabpot) January 10, 2009
This is Twitto: a web framework!
class µ{function __callStatic($n,$a){static $r;$s=$_SERVER;return $n=='_'?@$r[$s[REQUEST_METHOD]][$s[REDIRECT_URL]]():$r[$n][$a[0]]=$a[1];}}
— Stefano Azzolini (@lastguest) September 13, 2012
And this is a microframework created on the occasion of this “competition”. It’s called μ and you can find its documentation here.
These are three really nice examples of PHP code in a tweet. Of course you can’t use these two snippets in production, but you can see that a short script isn’t always an useless script or something prosy.
I realized the next two examples and would be great to see your tweets too!
function array_intersect_equal($a, $b) { return array_uintersect($a, $b, function ($c, $d) { return $c == $d ? 0 : 1; }); }
— Emanuele Minotto (@EmanueleMinotto) September 13, 2012
This is a function used to bypass the array_intersect
note “Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2
. In words: when the string representation is the same.”
function is_ajax() { return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); }
— Emanuele Minotto (@EmanueleMinotto) September 13, 2012
Using this you can check if a request is made using AJAX or is a simple web-client request. I think this could be a nice game (and a personal test) for each PHP developer.
Game rules
- The tweet have to be a well-formed PHP code. You can check the right syntax using
php -l tweet.php
as described here: php.net/manual/en/function.php-check-syntax.php - A code is taggable only using things like
echo "#php";
orreturn "#error";
but still keeping your tweet a well-formed PHP code - Open (
<?
and<?php
) and close (?>
) tags are not recommended
If you want suggest a ranking algorithm/formula, new rules or suggest methods to write the code, comment down here. For example you can evaluate comments, or the usage of some methods and classes, or evaluate more retweets than number of users that favorites your tweet, more the length or the utility, etc… I would be glad to see your tweets, so comment here linking your tweets!
Update of 27/08/2015
As you can see from the tweet below, this code golf game seems to be still alive, what about something more than a blog post?
Leave a comment and we can discuss about it, I think that something like 140byt.es or something more complex could be funny, and you? :)
<?php class S{static function __callStatic($k,$f){static $s;return $k=='get'?$s[$f[0]]:$s[$f[0]]=$f[1];}} https://t.co/lnDU9RujAy
— Fontana Lorenzo (@fntlnz) August 21, 2015