Solved

fixed preceeding 0's

Posted on 2014-10-18
4
123 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 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 110

Expert Comment

by:Asked Anonymously
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 110

Expert Comment

by:Asked Anonymously
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

740 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