Tuesday, March 24, 2009

And lo and behold, googling variable functions dug up another gem of PHP, variable variables.
Been working with PHP to build a small scale website - it's a refreshing change from ASP.net, and so happens to be the first programming language I ever learnt. Pretty interesting - given how I've been weaned to programming by Java, the differences between PHP and Java makes it an interesting experience (I'm repeating myself).

PHP is very strict on case-sensitivity, so.
Java is pretty strict as it is - classnames, methodnames, variables must be in the same case. PHP goes one up - array keys are also case sensitive. Coupled with the fact that I was working with a MySQL database (which is not case-sensitive at all) made for a frustrating bug-tracking session. (Live and learn eh?)

But the most interesting thing I stumbled upon is (*drumroll*) variable functions.
This is perfectly legal PHP -
function foo($string)
{
    print($string);
}
$func = "foo";
$func("bar");
?>
and it outputs "bar".

Essentially, you can put a function name as a string, and execute it by calling the variable and parenthesis. (It doesn't work with language constructs like echo or print, which is why $func = "echo";$func("bar"); doesn't work).

Now that could prove very very VERY useful. How so?
Imagine that you have to sanitize input, and you have three methods to do so, checkInt,checkStr and checkBool.

Normally you would call it as such :
if($type == "Str")
checkStr($str);
elseif($type == "Int")
checkInt($str);
else
checkBool($str);

Now, variable declarations allow you to do this :
$method = "check".$type;
$method($str);

See how much easier it is? (I haven't tested the above code, but I'm pretty sure it'll work).

Variable functions are really really interesting :D

Saturday, March 14, 2009

IT Fair 2009 today. Was packed to the gills. Boy.

And it's a preeety weird experience when you probably know more about the product's domain than the salesmen.

I've been shopping for 802.11n residential routers (cum ap cum switch. You get the point), and DD-WRT (http://www.dd-wrt.com/dd-wrtv3/index.php) support is pretty high up on my list of requirements.

Problem is, everywhere I go, I ask about DD-WRT support, and none of the salesmen has ever heard of it.

Sigh.

Was at a major technology supermarket previously, shopping for said 802.11n router (cum ap cum switch), and I wasn't sure about DD-WRT support. So, didn't purchase said router, but I did note down model and price.

Couple months later (ie. today), IT fair. Thought it was a perfect opportunity to get router so I went. Did some research on it. Thought the WRT160n was a safe bet - googling indicated that it did indeed, support DD-WRT (which was great, given that I wanted to bridge it with an existing router).

Went out, checked with the salesman(guy?). Couldn't be a day past 20 - probably a teenager during his holidays. But I digress. Checked with him about DD-WRT, didn't have a clue. Asked him about WDS (Wireless Distribution System), didn't have a clue, went somewhere to check. Came back and said yes.

So I got the router.

Went home, started setting it up, downloaded DD-WRT in anticipation, when I noticed that v1 and v1.1 of the 160n supports DD-WRT, whereas v2 does not. Looked under the router and naturally it was a v2. No luck for any custom firmware at the moment. Apparently they switched the broadcom with ralink chips. In fact, it looks like the CUT the specs on the v2. And guess what? There is no way to differentiate between the v1s from the v2s from the box, you have to open the boxes to differentiate the two.

Sigh. (At this moment I'm getting pretty dissapointed by Linksys products. First the WRE54g was a PITA to setup, then it was a PITA to configure and gave tonnes of problems while halving bandwith. Which was the whole point of the 160n - to get rid of the WRE and connect straight to the router.)

Alright, end of Linksys rant. (Note to self, do SERIOUS research before buying any more Linksys products).

Oh and, the router naturally didn't have WDS.

I hate salesmen. Those who don't know their stuff are pretty bad, but excusable. Those who try to lie their way through just to make a sale makes me pissed off.

Case in point : Was looking for a external portable harddisk for my laptop. Walked around a little, spotted the Western Digital booth. Wanted a harddisk with FireWire as speed was paramount for me. So.

Apparently the only portable harddisk that supported FireWire was the My Passport for Mac. I wanted that FireWire, so I checked with the staff.
"Oh hi, I was wondering, for the Mac version of My Passport, is it possible to actually format the harddisk so that I can use it in Windows?"
"Oh no, sorry."
"Why not?"
"OH YA YA can!"
Rinse and repeat for THREE salesmen/women who did the exact same thing. First query, not possible. Inquire more deeply and suddenly it's possible. And they didn't offer any explanation at all. I was the one who was asking them if I could actually format it from the HFS+ file system to NTFS. I'm willing to bet that they haven't even heard of these.

Geez, if you don't know, just say so. Don't fake it for the point of making a sale.

Naturally, I didn't get any harddisks.

And by the way, had I gotten the My Passport for Mac, I would have used my sis's MacBook Pro to format it to NTFS, then enjoy 320gb of storage space in all it's FireWire glory. Oh I just remembered. One of the salesmen actually tried discouraging me from getting the My Passport for Mac, and tried to get me to get the normal version (ie. Windows, non-FireWire version). The non-Mac one was $119 while the Mac one was 199. I didn't mind the price difference.
He said, and I quote : "Actually, why not get the other version? It's a price difference of 80 dollars, and FireWire is only 5-10 times faster than USB2.0, not worth it." *arches eyebrows*

Which brings me back to the point - it's pretty interesting, and somewhat frustrating when you know more about the product domain than the salesperson does.