<?php
// convert decimal to base 2 without using base_convert or printf
$base = 2;
$decnum = 23;
$num = 23;
$stack = array();
while( $num > 0)
{
$arr[] = $num % $base; // store remainder in array
$num = floor($num/$base); // no integer division
}
var_dump($arr);
$wrong = join('',$arr);
var_dump($wrong);
$cat = join('',array_reverse($arr));
var_dump($cat);
printf("%b\n",$decnum);
echo base_convert(23,10,2),"\n";
// now using the stack
while( $decnum > 0)
{
array_push($stack,($decnum % $base)); // store remainder on stack
$decnum = floor($decnum/$base); // no integer division
}
while($stack) {
echo array_pop($stack);
}
echo "\n";
// per manual: Note: If you use array_push() to add one element to the array it's better to use $array[] =
// because in that way there is no overhead of calling a function.
$decnum = 23;
while( $decnum > 0)
{
$stack[] = $decnum % $base; // store remainder on stack
$decnum = floor($decnum/$base); // no integer division
}
while($stack) {
echo array_pop($stack);
}
echo "\n";