9

I am working with an api and need to pass it the ClientID as an integer. The problem is that some of the ID's start with leading 0's. When I pass the integer to the API...PHP is cutting off the leading zero's which makes the ClientID inaccurate. I have tried passing the ID as a string to keep the zero's but the API expects an integer.

Example: ClientID = 00061423 when I pass it to the API it gets shortened to 61423 leading the request to fail because it can't find that client.

Is there a way to have PHP keep the leading zero's on integers?

Dustin Fraker
  • 524
  • 1
  • 5
  • 9
  • 3
    See [here](http://stackoverflow.com/questions/1699958/formatting-a-number-with-leading-zeros-in-php) – Jnatalzia Aug 09 '13 at 14:03
  • 2
    What API you are talking about? What is the interface it using? This string refusal story sounds strange to me – Your Common Sense Aug 09 '13 at 14:04
  • PHP doesn't do type-check; what technology does the API use? – Kneel-Before-ZOD Aug 09 '13 at 14:05
  • @YourCommonSense, you were right. The docs say that it has to be of type (int) but when I just passed it a string it worked just fine. I guess I should have just tried the obvious first. BTW it is mindbodyonline API and it uses SOAP. – Dustin Fraker Aug 09 '13 at 14:10
  • 2
    If the API genuinely both (a) expects an integer, and (b) expects it to have leading zeros, then the API is faulty. – Boann Aug 09 '13 at 14:12

3 Answers3

11

You cannot keep leading zeros in integer. You can either keep them in a string or add at output time, using sprintf(), str_pad(), etc.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
3

Use str_pad:

str_pad($var, $numZeroesToAppend, '0', STR_PAD_LEFT)
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
GGio
  • 7,563
  • 11
  • 44
  • 81
2

Pass it as a string; the zeros will be maintained.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26