...

View Full Version : Type Casting vs. ***val()



StupidRalph
08-05-2007, 06:54 AM
In which situations do you find yourself using type casting opposed to using the val functions?

I was just using the ctype_digit() and I have to pass a string into the function for it to work. And I didn't know if I should've used strval() to use the string value of my number OR if I should have type casted it to be a string when I declared the variable. (Afterwards its going to be inserted into into the database tinyINT field.) I guess strval() would be more appropiate in this situation no!?! I just didn't want to clutter up my if statement with another function.

StupidRalph
08-11-2007, 09:00 AM
No opinions?

ralph l mayo
08-12-2007, 07:21 PM
is_int/is_integer works with integer types as well as with strings. I don't know that I ever really cast anything in PHP. I use is_integer, is_numeric, and the very occasional regexp for validation (in PHP anyway, I make the database enforce a lot more constraints.)

Inigoesdr
08-12-2007, 09:00 PM
I generally typecast, but it's my understanding that either way will produce the same results.

StupidRalph
08-13-2007, 07:29 AM
Oh ok I just wanted others opinions.

I thought the strval() would be more appropriate in this case b/c my variable really should be a integer that is passed to that specific function as a string. Although, I use sprintf and format as a integer while being inserted into the DB. I didn't want any scientific notation or hex values so I didn't use is_numeric. Don't quote me but I think is_int() could be either a numeric string or a number which isn't what I wanted.

Thanks ralph_l_mayo and Inigoesdr.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum