Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Type Casting vs. ***val()

    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    No opinions?
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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.)

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    I generally typecast, but it's my understanding that either way will produce the same results.

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •