PHP variables, strings, and curly braces

For the last couple of days we had a number arguments at work about what is the best way to surround a complex PHP variable inside a double-quoted string.  More specifically, should the sigil ($, dollar sign) be on the inside of the braces or on the outside.  Consider an example:

# my way
echo "Result: ${blah['something']}\n";
# the highway
echo "Result: {$blah['something']}\n";

While considering a number of examples, there seems to be no difference – both ways work.  We’d still need to pick one for consistency reasons though.  And I, as an ex-Perl programmer, was suggesting that we should use the dollar sign on the outside of the expression.  This how I remember it being in Perl (and PHP originated from Perl) .  This is how I am used to it.  And this is how makes most sense to me – a dollar sign immediately warns the programmer that the variable is ahead.

However, after consulting PHP documentation, I was proved wrong.  It is said that both ways often work, but it is much safer to use the dollar sign on the inside.  The manual page even provides a few examples where the dollar on the outside won’t work (such in case with objects).

While this is just a small thing to know and get used to, it still looks annoying to me.

The amazing Chromium

Being a web worker, I spend a lot of time in my browser. Over the years, I’ve used pretty much everything – from early versions of Netscape and Microsoft Internet Explorer, to text-mode browsers like lynx, links, and w3m, through a whole bunch of Linux desktop browser like Konqueror and Galeon, through mobile browsers, through the latest versions of Opera and Mozilla Firefox. And out of all that variety, Mozilla Firefox was pretty much my only true love browser. It was reasonably fast, free, open source, and with a billion of extensions.

In fact, I accumulated so many extensions that I had to group them into four different Firefox profiles. With all those extensions, I tweaked and tuned my browsing experience to exactly match my needs. And I was happy for a long time. And I couldn’t even think of leaving Firefox for another browser.

Lots of people complained (and still do) about Firefox stability. But I never had any major issues with it. My sessions were stored and backed up automatically, and in those rare cases when the browser crash, I’d just start a new instance and it would automatically open all windows and tabs and bring me back up to speed all by itself.

Most people complained about Firefox performance. Well, I can understand them. It does feel slow and sluggish sometimes, but I was thinking of it as a rather cheap price to pay for all the extensions that I had. Opera, for example, is a much faster browser, but doesn’t have even 1% of the extensions that I use. And that makes it pretty much useless to me. No matter how fast it is.

So I was mostly happy with my browser. I loved it and it loved me back. And then Google released their vision for a modern browser – Chromium. At first, it didn’t even run on Linux, which is my preferred desktop operating system. So I didn’t bother. Then it ran, but people were saying that there were no extensions what so ever. So I didn’t bothered again either.

But the hype was growing, and people were shouting all over the web about how fast and how convenient Google Chrome is. So I just had to try. I thought, I’d download and install it, and use it for a couple of days, just to get my own opinion of it. That was the very beginning of December 2009 and I wasn’t even remote thinking of switching to another browser.

Now that it’s almost middle of February 2010, two and a half month later, I have to confess. I switched to Google Chromium. But that’s not the most surprising thing ever. What’s extremely surprising, at least to myself, is that I switched to Chromium the first day I tried it. Without even knowing.

It was indeed blazing fast. Super-fast. Super-sonic even. Convenient – yes, but it was way faster than I though was even possible for a browser. Extensions were missing, but I was saving so much time with the fastest browser experience ever, that I could do anything my extensions did manually, and would still have plenty of time left. I was shocked and hooked.

And about two weeks later, when I just started to get used to how fast a browser can be, Google opened up their Chromium extensions site. So now I also had some extensions to install. And which I did. And there was no going back.

To this day, Chromium is my default, primary, and mostly preferred browser. It does still have a few shortcomings and things that I’d want different, but all of them are nothing compared to the performance boost that this browser brought into my web life.

PHP date() and 53 weeks

Let’s say you have a bunch of statistical data.  And all that data is date-related.  And let’s say want to display that data on a chart, a weekly average or something along those lines.  One of the ways for you to place the value into the proper week would be something like this:

$week = date('W', strtotime($stats_date));
$values[$week][] = $stats_value;

And if you did it this way, sooner or later, you’d notice that something is not quite working right at the edges of your chart.  With code as simple and straight-forward as this, you’d probably look for the problem elsewhere.  Maybe it’s your statistical data which is wrong, or the graph is not generated properly.  But the problem is here.

How many weeks do you think there are in a year?  A common knowledge says 52.  However, if you think for a moment about how the weeks are related to the year, you’ll realize that the first and last weeks don’t necessary start and end at the edge of the year.  If you play around with 1st of January and 31st of December across several years, you’ll notice that sometimes they fall into the 53rd week.  (As do a few more days, not just these two).

And here, the problem with the “W” date() format starts to emerge.   When scattering your data across a single year, you’d most often expect January to start with the first week of the year.  But it doesn’t. Sometimes the start of it falls into the 53rd week of the previous year.  And date(‘W’, $your_time) will happily return 53.  What will this do to your chart?  Two things are most likely.  The first week’s values would get reduced, and the last week’s values would get increased.  Or those values of the first week would altogether vanish from the graph.  That is unless you are careful.  Which I hope you’ll now be.

See comments to PHP date() manual for several solutions of this problem.