Solved

fixed preceeding 0's

Posted on 2014-10-18
4
109 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCPDF HTML Table of Contents how can I create leading dots 7 35
php ssh2_scp_send 1 46
wamp versus xampp 4 43
Why my select dropdown does not work? 8 21
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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