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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    Convert number as string to number?

    I thought php had parseInt or something of the sort (as in javascript).
    ......
    I just figured out how to use settype....

    false alarm

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP automatically converts based on context. So adding two strings of numbers results in a number. This is IMO a tremendous weakness of using datatype weak languages.
    For explicit, I'd cast directly using (int) or (float) if you need to. Settype is a tad on the temperamental side. Even using an intval() would be better.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by anotherJEK View Post
    I thought php had parseInt or something of the sort (as in javascript).
    The PHP equivalent to JavaScripts parseInt() is base_convert() which is even more flexible since parseInt can only convert to base 10 from bases between 2 and 36 whereas base_convert can convert between any two number bases.

    So JavaScript:

    Code:
    $num = parseInt('f',16);
    PHP equivalent

    Code:
    $num = base_convert('f', 16, 10);
    Neither has anything to do with converting strings to numbers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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