?
Solved

fixed preceeding 0's

Posted on 2014-10-18
4
Medium Priority
?
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 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 111

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 111

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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