So, it's pretty cool that you can use positional arguments with sprintf.

   $retval = sprintf('%2$s is %1$s', 'awesome', 'Evan');

It's important to remember, though, that /inside double quotes/ that $s will be treated as a /variable,/ which will be (usually) unset, which will result in something like '%2 is %1'. sprintf() will ignore the escape codes, resulting in ' is '. Which isn't what we wanted.

To deal with this problem, I typically do one of two things:

   * Replace double quotes with single quotes. In fact, I try to do
     this /by default for all strings/, unless I consciously want to
     have variable interpolation or need to use C-style escapes like
     \t, \n, or \v.
   * Escape the $ char so that $s isn't interpreted as a variable.
     You'll see this in code in StatusNet where we need \n in the
     string. So,

       sprintf("%2\$s is %1\$s", 'awesome', 'Evan');

I realize this is kind of boring, but I just removed a bunch of bugs from StatusNet where we had this kind of interpolation and page titles were showing up incorrectly.

-Evan

--
Evan Prodromou
CEO, Control Yourself, Inc.
e...@controlyourself.ca - http://identi.ca/evan - +1-514-554-3826

_______________________________________________
StatusNet-dev mailing list
StatusNet-dev@lists.status.net
http://lists.status.net/mailman/listinfo/statusnet-dev

Reply via email to