Solved

fixed preceeding 0's

Posted on 2014-10-18
4
104 Views
Last Modified: 2014-10-22
Hi,
I am using Ckaephp but php will do for this.
I just need to know how to store preceding 0's in front of a integer variable so I have always 6 digits.

I saw sprintf but I dont know how to keep 6 digits to a number always.

$InvoiceNo =1;  but I want $InvoiceNo =000001;
I also want to add to this number $InvoiceNo +=1; will append the 0's and 10 wil be 000010

 $this->request->data['TimesheetDate'][$key]['invoice']=$InvoiceNo ;  will add this number to the DB and this currently works without leading 0's
0
Comment
Question by:jagguy
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40389473
http://php.net/manual/en/function.str-pad.php

str_pad($the_number, 6, "0", STR_PAD_LEFT);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40389505
The problem with this question is that it overlooks the essential "loose-typing" nature of PHP.  Here's an example of how you can try to do the right thing and still get screwed:

$num = str_pad($the_number, 6, "0", STR_PAD_LEFT);
$num++;
echo $num; // NOT WHAT YOU WANT AT ALL!

PHP silently converts data elements into representations that are appropriate for the context in which they are used.  In arithmetic, character strings may be treated as if they were numbers.  In printing, numbers may be dynamically converted to string.  Don't even go there with arrays and objects!

Executive summary: If you want to echo a padded numeric value, do the string padding immediately before you echo the value.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40389513
What Ray said.  ^^^ As soon as you do anything mathematical to a PHP variable, leading zeros (and trailing text!) are Gone.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40396592
PHP str_pad() is a good function, but only if you use it at exactly the right time.  What is that time? For anyone coming across this question in the future, please read the response here:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28540130.html#a40389505
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now