Solved

fixed preceeding 0's

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

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
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.

789 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