intval returns negative value

Discussion in 'PHP' started by x0x, Sep 16, 2008.

  1. #1
    Can intval be forced to positive or 0, let's say if it's negative, it would be forced to 0.
     
    x0x, Sep 16, 2008 IP
  2. ricetown

    ricetown Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    why don't use some codes like
    if( $i<0) $i = 0;
     
    ricetown, Sep 16, 2008 IP
  3. eugen

    eugen Active Member

    Messages:
    154
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Nope, intval function is returning any positive or negative number between -2147483648 and 2147483647.

    But, you can write your own function like:
    
    function myIntVal($number)
    {
       if ( $number > 0 )
       {
          return intval( $number );
       } else {
          return 0;
       }
    }
    
    Code (markup):
    Hope this is want you wanted.
     
    eugen, Sep 16, 2008 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Yeah, thank you.
     
    x0x, Sep 16, 2008 IP