0

--- with leading zeros is different to without losing ... ---

I have to create a path made by values. One value is par example a number with the value "00123345". At the end of the process he will give a value "123345". But the value has to be "00123345" instead of "123345".

The number has not a fixed length. The number who will be given, has to be used as is given. "123" results in "123" and "00123345" results in "00123345" and "0125" results in "0125".

What do I wrong?

Input Number = "00123345";

$Code = $_GET("Number");

public function __construct($code)
{
   $this->Code = $code;
}

private function GetLocalDirectory()
{ 
   $Response = "";
   if ($this->Code != "")
   {
       $Response = ... . "/" . $this->Code . "/" ...;
   } 

   return $Response;
}

$path = $this->GetDirLevel($path, $this->Code);
user1531040
  • 2,143
  • 6
  • 28
  • 48
  • Do you always want to have e.g. 8 digits (then it is indeed a duplicate). Or do you want to be able to set the `"Number"`-variable to `00123` or `0123` and keep `00123` and `0123` exactly as it was? (Then the php automatic type declaration is giving you trouble, but you can e.g. use `settype` to force it to be a string.). – Solarflare Dec 19 '16 at 17:01
  • Formatting a number with leading zeros in PHP is not the question. WITHOUT LOSING leading zeros in a Number. – user1531040 Dec 21 '16 at 13:18
  • @Ryan. What do you mean exactly? I think about something that you read it as a text format... Is that possible? How do you do that? It seams it is read as a numeric value. – user1531040 Dec 28 '16 at 10:35
  • So you have placed every where you use the "Number"-value. Because my solution see no difference between 00123345 and 123345. And we use that "Number" as a folder. In both applications it is a identification value. – user1531040 Dec 28 '16 at 12:23

1 Answers1

0

You have to treat it as a string and pad zeros like this:

$this->Code = str_pad($code, 8, "0", STR_PAD_LEFT);
sg-
  • 2,196
  • 1
  • 15
  • 16
  • Thank you for your answer. The number has not a fixed length. The number who will be given, has to be used as is given. "123" results in "123" and "00123345" results in "00123345" and "0125" results in "0125". – user1531040 Dec 19 '16 at 11:48
  • Ok then I don't get what you're asking because you said "At the end of the process he will give a value "123345". But the value has to be "00123345" instead of "123345" – sg- Dec 19 '16 at 11:56
  • The example starts with: "I have to create a path made by values. One value is par example a number with the value "00123345" ...." – user1531040 Dec 19 '16 at 12:06
  • You lost me, I'm out. – sg- Dec 19 '16 at 12:10
  • Okay. I'm not a real php-programmer. For me everything is new. And still I had to solve this... – user1531040 Dec 19 '16 at 12:20