views:

305

answers:

5

Can someone tell me why when I cast a string of say 00332 I only get back 332? It removes the leading zeros and saves the data in the same format.

Thanks


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

What do you cast it to and why? Let's see the code. If you cast to integer, yeah, integers don't have leading zeroes, so they disappear. No surprise.

Michael Borgwardt
Please have a look above. Thanks for the help
A: 

I'd say because the leading zeros don't have any importance when cast to an integer.

Maybe you should leave it as a string if you need those leading zeroes, and only cast (int) on any math needed to be performed (though you could just use the string too, PHP will figure it out)

edit

After seeing the example, I'd want to echo the value before you cast to string (so I can confirm something fishy isn't going on with you QuoteSmart method (or the value your sending as a param, $gate)

alex
Hey Alex. You heloed me the other day, Thanks for the help! I posted above a sample.
The val I'm sending is 00432 and after its cast, I only wind up with 432 being saved in the database. :/ So, prior to casting, I can echo 00432 and afterwards only get 432. Let me remove the quotesmart function and see if that fixes it. brb
Also, Jim, what type is on the field of your DB ? This could be affecting what is saved in the DB too.
alex
It will need to be something like VARCHAR, not an INT variety (which will most probably destroy useless information to an integer, such as leading `0`s)
alex
Well, I like how your thinking Alex but the field is set to char which will accept any type of input.
Well, if you're only reason for casting to string here is for security, I would ignore it. I perform all security related things on external vars as they come in, first and foremost. So either your var is secure already, or you're using register_globals, which isn't good.
alex
Just curious, which other question did I help you with ? I can't recall (Saturday morning here...)
alex
I had a PHP question and for the life of me, I cannot remember what exactly it was. You came through for me though. It was only a couple of days ago.
+2  A: 

Don't cast strings to strings, or any type to the same type for that matter. Typecasting is for changing something from one type to another.

Marc W
@Jim, it really depends, if you're accepting a user input which MUST be a number (example, a $_GET['id']) then it'd be a good idea to cast to an integer with (int)$_GET['id']. It will blow away any leading alpha stuff, and if it can't get to any useful numbers, will become 0.
alex
However, other precautions need to be taken depending on the context... example, vars printed to page should be htmlspecialchars()'d and vars in a database query should be mysql_real_escape_string or the equivalent for your DB. In fact, just use parameterised queries. Check out www.php.net/pdo
alex
Ok, that makes sense.. I think Alex may have a point about the quotesmart function changing the output. I am going to remove it and see what I get.
Of course, there's a whole lot more security things to be taken into account for, but luckily there is a lot of stuff on the web. Research wisely!
alex
OK, removing quotesmart did nothing. Here is what I have now:$this->_gate = (string) $gate;0433 returns 433. This field can be a mixture of string or int.
Check out my comment to my answer Jim, ensure your DB has the right type for your column.
alex
I saw that Alex. Thanks. I am using char. I'm tracing the var from the html form into my class right now and inside the methid, I can still echo the proper value. If I edit mysql directly and put a val of 003322 it saves it as exactly that so I'm confused as to what's going on here.
+1  A: 
$ php -r 'var_dump((string)"00123");'
string(5) "00123"

Looks like the cast isn't your problem.

Frank Farmer
Thanks for that. It was what got me looking for the problem elsewhere. :)
A: 

OK, here is why it was dropping the leading zeros. It was NOT casting nor addslashes. What I did was to quote the variable (which quotesmart should be doing anyhow but didn't for some reason) Once I quoted the variable, I got the value saved correctly. If Im not mistaken, quotesmart will only work on strings and not ints.