Cool PHP-Code.

Did you know that in PHP you can write something like that:

$test = „foobar“;
$test = sTr_RePlace(„bar“, „baz“, $test);
$x = sPrinTf(„%s is strange.“, $test);

pRint $x . „n“;
eCho „foo“;

What makes me frighten is that this actually used, e.g. by this code snippet which exists (in a similar form) in an unnamed PHP-project:

echo sPrintF(_(„Bla bla bla: %s“), $bla))

And yes, they do use echo to output the result of sprintf.

Update: So i got this great comment. The commentator wants to point out that the senseless use of „echo sprintf“ is because of gettext. He says „That’s simply the way you use gettext.“ But this is simply not true. The difference between printf and sprintf is that the first one outputs the string, while the second one returns it. That means that in the above example printf could be used (instead of sprintf) without a useless echo call in front of it. The reason for using sprintf (and eventually the reason because you find it in a lot of applications using gettext) is that you can use it to fill a variable with the translated string or to use the string in-place. A common use-case for this is to handle a translated string to a template engine, for example.

4 Gedanken zu „Cool PHP-Code.“

  1. Hrm … i guess, I should use the following in my C code from now on – just to be prepared for using gettext:

    #define echo(str) printf("%s", str)

    char *snPrintF(char *str, size_t size, const char *fmt, …) {
      va_list ap;
      va_start(ap, fmt);
      vsnprintf(str, size, fmt, ap);
      return str;


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.