note 68671 added to function.log

From: Date: Sun, 06 Aug 2006 12:56:58 +0000
Subject: note 68671 added to function.log
Groups: php.notes 
Request: Send a blank email to php-notes+get-115591@lists.php.net to get a copy of this message
<?php #-------------------------------------------------------- # How many digits does an integer have? #-------------------------------------------------------- function digit_count($n, $base=10) { if($n == 0) return 1; if($base == 10) { # using the built-in log10(x) # might be more accurate than log(x)/log(10). return 1 + floor(log10(abs($n))); }else{ # here logB(x) = log(x)/log(B) will have to do. return 1 + floor(log(abs($n))/ log($base)); } } # Example: How many decimal digits for 2 to the power 24? echo digit_count(pow(2, 24)); # Example: How many bits to write 1 billion in binary, last century? if($country_code == 'US') echo digit_count(pow(10, 9), 2); if($country_code == 'UK') echo digit_count(pow(10, 12), 2); #-------------------------------------------------------- # Using log to format columns. #-------------------------------------------------------- # Suppose we have a dynamically generated list of integers, # and want to present them as a table. The use of log10 in # our digit_count helps calculate the proper format string. function print_list_of_ints($ints, $line_width=40) { # Apply our digit_count to the max int among ints. $field_width = 2 + digit_count(max($ints)); # Create format string for printf. $format = "%${field_width}d"; $ints_per_line = floor($line_width/$field_width); $border = str_repeat("-", $ints_per_line * $field_width); echo "\n$border\n"; foreach($ints as $count => $int) { if( $count and ($count % $ints_per_line == 0)) echo "\n"; printf($format, $int); } echo "\n$border\n"; } # To generate an example, here is a basic function # returning a list of (pseudo) random numbers. function rands($how_many) { for($i=0; $i < $how_many; $i++) $rands[] = rand(); return $rands; } # Example: A list of random ints dynamically formatted into columns. print_list_of_ints(rands(11)); /* Sample output. Numbers and fonts vary. Visualize monospace! ------------------------------------ 1093146637 244503173 1346204527 638304372 140216732 1054707210 573915416 1728677954 2038899669 534854768 12674586 ------------------------------------ */ ?> ---- Server IP: 64.71.164.2 Probable Submitter: 66.92.9.47 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.log.php Edit -- https://master.php.net/note/edit/68671 Del: integrated -- https://master.php.net/note/delete/68671/integrated Del: useless -- https://master.php.net/note/delete/68671/useless Del: bad code -- https://master.php.net/note/delete/68671/bad+code Del: spam -- https://master.php.net/note/delete/68671/spam Del: non-english -- https://master.php.net/note/delete/68671/non-english Del: in docs -- https://master.php.net/note/delete/68671/in+docs Del: other reasons-- https://master.php.net/note/delete/68671 Reject -- https://master.php.net/note/reject/68671 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115591) next »