Casting removes leading zeros

Can anyone tell me why when I say the line with the message 00332 I only go back? It removes leading zeros and keeps the data in the same format.

thanks


this->_gate   = (string) $this->_linkID->QuoteSmart($gate);

      

+1


a source to share


5 answers


I would say because the leading zeros have no meaning when passed to an integer.

Maybe you should leave it as a string if you want those leading zeros, and only cast (int)

for whatever math you need to do (although you can just use a string too, PHP will figure it out)



change

After looking at the example, I want to echo the value before you scribble on the line (so I can confirm that something fishy isn't happening to you with the QuoteSmart method (or your send value as a parameter, $ gate)

+1


a source


Don't cast strings to strings or any type to the same type. Typing is changing something from one type to another.



+2


a source


$ php -r 'var_dump((string)"00123");'
string(5) "00123"

      

It looks like the cast isn't your problem.

+1


a source


What are you referring and why? Check out the code. If you drop an integer, yes, integers don't have leading zeros, so they disappear. Not surprising.

0


a source


OK, that's why it was dropping leading zeros. It was not cast and does not add. What I did was quote the variable (which quotes are supposed to do anyway, but not for some reason). When I cast this variable, I saved the value correctly. If I'm not wrong, quotesmart will only work with strings, not ints.

0


a source







All Articles