_
_ Handy PHP
_
_
Tutorials, Scripts, Information And Other Resources arrow Forum
Thursday, 24 July 2008
_

Refer A Friend using Revolution Money Exchange
Resource Menu
Handy PHP Store
Free Domain Name
PHP Functions
PHP Downloads
PHP Newsfeeds
PHP Tutorials
Webmaster Tools
User Login
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index
Handy PHP
Free PHP Help!

FireBoard
Welcome, Guest
Please Login or Register.    Lost Password?
Re:Leading Zeros (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Leading Zeros
#1
vujsa (Admin)
Admin
Posts: 138
graph
User Online Now Click here to see the profile of this user
Leading Zeros 1 Year, 9 Months ago Karma: 4  
This topic is for the discussion of the Handy PHP function Leading Zeros.

Please keep the discussion related to this function.
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#266
TheDarkestDay (User)
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
Re:Leading Zeros 9 Months, 1 Week ago Karma: 0  
Wouldn't it be easier to just do this?
Code:

function leading_zeros($value, $places){ if ( is_numeric ( $value ) ) { $Type = gettype ( $value ) ; settype ( $value, 'string' ) ; while ( strlen ( $value ) < $places ) { $value = "0{$value}" ; } settype ( $value, $Type ) ; } return $value ; }
You can even pass numerical values that aren't strings to it.<br><br>Post edited by: TheDarkestDay, at: 2007/10/14 14:13
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
#305
dblosevn (User)
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
Re:Leading Zeros 6 Months, 2 Weeks ago Karma: 0  
Or even simpler.
Convert $value to string first. Add the correct amount of 0's and append $ value.

Code:

function leading_zeros($value, $length = 2) { $value = (string) $value; $output = str_repeat('0', $length - strlen($value)); $output .= $value return $output; }
 
Report to moderator   Logged Logged  
  The administrator has disabled public write access.
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

_
 
_
_
© 2008 Handy PHP