In these blog series I'd like to talk a bit about some "Did you knows". These series contain information I came across along the way and I mention them here to give you insight or just to make you aware of it's existence. The information is by no means in chronological order and mostly not even covering "state of the art" or "brand new" items for that matter. Some are directly code related, others are just brief descriptions. Basically it's just a pile of PHP and web related information. You can find part one here.
Numeric range sanitizing, min() and max()
For easy range sanitizing I like to use min() and max(), it's nice and clean:
// $alpha will always be be in the range: 0.01 till 1.0 $input =
(rand(1,
200) /
100);
$alpha =
max(0.
01,
min(1.
0,
$input));
// Even with any of these values, even though they might give unexpected results it's still a valid input:$input =
"fubar";
$input =
6;
$input =
array(0,
1,
2,
4);
$input =
new stdClass;
string scanning, sscanf
Ever since sscanf() and I met, I fell in love. I love sscanf it's nice, fast and in a lot of cases it suits the need and avoids the use of (sometimes more complex) preg_* functions
$input =
'31-12-1951';
$date =
sscanf($input,
'%d-%d-%d');
// $date[0] 31// $date[1] 12// $date[2] 1951$input =
'31-12';
$date =
sscanf($input,
'%d-%d-%d');
// $date[0] 31// $date[1] 12// $date[2] NULL/** Or something like: **/sscanf('amount( 17 )',
'amount(%d)',
$amount);
// $amount is a integer with the value: 17 (despite the white space)
Referenced foreach values
When using referenced values in loops, make sure you unset it, if you don't you can get situations like this:
$array =
array(0,
1);
foreach($array as &
$value) { $value +=
10;
} $value =
"a";
echo $array[1];
// output "a" rather then: 11 Other then that, it can be a nice way of updating arrays when one of the array map/walk functions aren't powerful enough.
strtok
In PHP 5.3, strstr() get's a third argument to return the part before the needle. This behavior however can already be accomplished with the function strtok()
$domain =
"php.net";
// or "php.co.uk"$first =
strtok($domain,
'.');
// Which is much nicer (and more useful) then:$first =
substr($domain,
0,
strpos($domain,
'.'));
// And definitely much nicer then:$first =
reset( explode('.',
$domain) );
Date and time
In "Did you know... part one" I talked about Derick Rethans and his DateTime book, back then it was still in the making. But it has finally been released! So if you often work with dates and/or just like to read a little history about time zones, be sure to check it out! http://phpdatebook.com/.
Nginx
There is more to the land of HTTP servers then the giants such as Apache and IIS. There are quite a few smaller (and sometimes better) servers out there. One of them is the open source application: Nginx. Nginx is a HTTP server written by the Russian Igor Sysoev and is the heart for many huge sites, such as Wordpress or Github. Nginx is build with performance and easy configuration in mind and comes with a whole set of modules and 3rd party modules, called: addons, covering most of your needs. And you can also build your own addons to satisfy your custom needs.
BOUML
BOUML is a UML tool box, with support to create UML from PHP code. Generally you should do it the other way around, design then build. But I'm very happy with the reverse-engineer feature. It's free software, it's fast and I can do anything with it that I expect from a UML tool. It's not as fancy as similar commercial product (like for example: Enterprise Architect). But it get's the job done, which is what I care most about with these kind of products. And above all, it has very good PHP support!