Alpha/Numeric problem.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Promextheus Xex

    Alpha/Numeric problem.

    I'm having a problem with numbers. I'm getting a number from an array that
    seems to have a padded space at the beginning.

    IE: " 5"

    What I've done was $var = ltrim($var) which removes the space but with one
    problem. Now I think PHP thinks it's a sting and not a number.

    Then I try $var = settype($var, "integer") to see if it will become a
    numeric for addition purposes.

    Next problem... $var is now 0 and gettype($var) shows its and integer.

    Why would it be 0 and not 5? Perhaps there's an obvious starring right at
    me and I'm not seeing it?

    - Francis


  • Promextheus Xex

    #2
    Re: Alpha/Numeric problem.

    Oops... I made a typo (not a mistake in my code):

    "Then I try $var = settype($var, "integer") to see if it will become a "

    should have been:

    "Then I try settype($var, "integer") to see if it will become a "
    ^
    (This is what I have actually coded and should have typed in my post.)

    Comment

    • Michael Fesser

      #3
      Re: Alpha/Numeric problem.

      ..oO(Promextheu s Xex)
      >I'm having a problem with numbers. I'm getting a number from an array that
      >seems to have a padded space at the beginning.
      >
      >IE: " 5"
      >
      >What I've done was $var = ltrim($var) which removes the space but with one
      >problem. Now I think PHP thinks it's a sting and not a number.
      >
      >Then I try $var = settype($var, "integer") to see if it will become a
      >numeric for addition purposes.
      Usually you don't have to explictly set a type. Just use the variable
      as-is (even if there's leading white space, it shouldn't matter), PHP
      will automatically convert it when necessary.

      $var = ' 5';
      print $var+10;

      outputs 15. If you still want to do an explicit typecast, just write

      $var = (int)$var;

      That's enough. There's need for ltrim() and settype() in this case.
      >Next problem... $var is now 0 and gettype($var) shows its and integer.
      >
      >Why would it be 0 and not 5? Perhaps there's an obvious starring right at
      >me and I'm not seeing it?
      There's definitely something wrong, if you get a 0. What version of PHP
      do you use?

      Micha

      Comment

      • Geoff Berrow

        #4
        Re: Alpha/Numeric problem.

        Message-ID: <45744754$0$178 67$c3e8da3@news .astraweb.comfr om
        Promextheus Xex contained the following:
        >I'm having a problem with numbers. I'm getting a number from an array that
        >seems to have a padded space at the beginning.
        >
        >IE: " 5"
        >
        >What I've done was $var = ltrim($var) which removes the space but with one
        >problem. Now I think PHP thinks it's a sting and not a number.
        PHP has loose typing and can easily deal with that.

        e.g.

        $string1 = " 5";
        $string2 = 2;
        echo $string1+$strin g2;

        //outputs 7

        Your problem is elsewhere.

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        Working...