-5

Remove the preceding zero from a string

000001 to 000010

It stores the following values

00001, 2, 3, 4, 5, 6, 7, 8, 8, 10

But I want something like this.

00001, 00002, 00003, 00004, 00005, 00006, 00007, 00008, 00009, 000010

The algorithm is removing those zeros from start. Please tell me how to do that.

Mohsin
  • 75
  • 11
  • 2
    "you are trying" lets show code – Jacek Cz Sep 07 '17 at 10:12
  • 2
    _The algorithm is removing those zeros_ What algorithm? And why would you need any leading zeroes? – B001ᛦ Sep 07 '17 at 10:13
  • 4
    Remove zeros or adding zeros ? Try `str_pad()` – Raptor Sep 07 '17 at 10:14
  • 2
    In the integer value 001, the 00 is redundant, why it removes it. If you wish to store it as 001, you should store it as a string. – Jite Sep 07 '17 at 10:14
  • I want those zeros because they are the part of the serial of some products – Mohsin Sep 07 '17 at 10:15
  • I can't store it as string. the complete scenario is this that user will enter the starting serial and ending serial and all the products were saved into the database with the increment of 1 in each product. so if I use string then I can't add 1 into it – Mohsin Sep 07 '17 at 10:18
  • 1
    a number where leading zeroes are significant is *not* an integer, but is a *numeric string* instead. – Calimero Sep 07 '17 at 10:26

2 Answers2

2

This one's easy! Try this:

<?php

$num = 5;
echo str_pad($num, 6, '0', STR_PAD_LEFT);

Outputs 000005

See it here https://3v4l.org/0Fgdp

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

If no of zeros are fixed then:

str_pad($your_no, $total_no_of_digits, $string_to_append, STR_PAD_LEFT);

else, you can use the associative array to define no of zeros then use str_pad()

Qirel
  • 25,449
  • 7
  • 45
  • 62
Vijay Maurya
  • 99
  • 1
  • 6