Regarding trailing zeros, after test all the option mention here by others, i have performed my own tests regarding efficiency, here are the results:
<?php
$decimal = 9;
$time_start = microtime(true);
for ($i=0;$i<1000;$i++){
$bin = printf('%08b', $decimal);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<hr>Duracion $time segundos<br>\n";
echo $bin . '<br>';
$time_start = microtime(true);
for ($i=0;$i<1000;$i++){
$bin = sprintf( "%08d", decbin( $decimal ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<hr>Duracion $time segundos<br>\n";
echo $bin . '<br>';
$time_start = microtime(true);
for ($i=0;$i<1000;$i++){
$bin = decbin($decimal);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<hr>Duracion $time segundos<br>\n";
echo $bin . '<br>';
?>
results
0000100100001001000010010000100.... (output is echoed 1000 times)
Duracion 0.0134768486023 segundos
8
Duracion 0.00054407119751 segundos
00001001
Duracion 0.000833988189697 segundos
00001001
Thus the winner is
<?php
$bin = sprintf( "%08d", decbin( $decimal ));
?>