5 useful (PHP) tricks and functions you may not know

I’ve been programming in one language or another (but mostly PHP) for a good few years now, and I’m still discovering new functions and coding tricks to make my life easier. I’m going to share an assorted collection of some of my favourite revelations - see if there’s any you’re not familiar with:

1. http_build_query (PHP)

For the longest while, whenever I wanted to turn a native array into a nicely-encoded query string, I’d run it through a userland function that’d sequentially implode and concatenate each key-value pair. I had no idea there was a native PHP function that’d do it all for me! Aside from the obvious boons of speed and portability, the native function is configurable and fully supports nested arrays, something my own version never did.

2. <input name="foo[bar]" />

Following on from the above, I didn’t initially realise that HTML+PHP are quite capable of handling form fields as arrays. This one’s particularly helpful when dealing with multiple checkboxes since the selected values can be automatically pushed into an indexed (or associative) array, rather than having to capture them yourself.

3. Boolean Inversion

Most of the time, inverting a boolean value is as simple as using the logical ‘not’ operator e.g. false = !true. That’s easy enough, but occasionally you might find yourself working with integer-type booleans instead, with 1s and 0s in the place of true and false; in that case, here’s a short PHP snippet that does the same thing:
$true = 1;
$false = 1 - $true;
$true = 1 - $false;
The same principle can be used any time you want to toggle an integer between two values e.g. between 2 and 5:
$val = 5;
$val = 7 - $val; // now it's 2...
$val = 7 - $val; // and now it's 5 again
The only system I regularly use that lacks native boolean support is MySQL but, fortunately, it manages to work around that.

4. isset($var, $var, …) (PHP)

Useful little thing, this - you can check the state of multiple variables within a single PHP isset() construct, like so:
$foo = $bar = 'are set';
isset($foo, $bar); // true
isset($foo, $bar, $baz); // false
isset($baz, $foo, $bar); // false
On a related note, in case you’re not already aware of this, isset actually sees null as being not set:
$list = array('foo' => 'set', 'bar' => null);
isset($list['foo']); // true, as expected
isset($list['bar']); // false!
In situations like the above, it’s more reliable to use array_key_exists().

5. Modulus Operator

During a loop, it’s a fairly common need to perform a specific routine every n-th iteration. The modulus operator is extremely helpful here - it’ll divide the first operand by the second and return the remainder, to create a useful, cyclic sequence:
for ($i = 0; $i < 10; ++$i)
    echo $i % 4, ' ';
// outputs 0 1 2 3 0 1 2 3 0 1
We could, for example, test ($i % 4) == 3 to put a <br /> after every forth element of a list. I’ll end the list here for today. There’s several more tricks I’d like to relate but, in the interest of berevity, I’ll save them for next time!