I was just taking a look at CodeIgniters encryption class and found the following function:
function sha1($str)
{
if ( ! function_exists('sha1'))
{
if ( ! function_exists('mhash'))
{
// use CI implementation
require_once(BASEPATH.'libraries/Sha1'.EXT);
$SH = new CI_SHA;
return $SH->generate($str);
}
else
{
// use alternate built in function
return bin2hex(mhash(MHASH_SHA1, $str));
}
}
else
{
// use built in function
return sha1($str);
}
}
If I was writing this function I would do so like this:
function sha1($str)
{
// use built in function
if(function_exists('sha1'))
return sha1($str);
// use alternate built in function
if(function_exists('mhash'))
return bin2hex(mhash(MHASH_SHA1, $str));
// use CI implementation
require_once(BASEPATH.'libraries/Sha1'.EXT);
$SH = new CI_SHA;
return $SH->generate($str);
}
Which is better and why?
if (!x) {} else {}drives me nuts. – nicodemus13 Dec 20 '12 at 13:45