SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot cools_sonu's Avatar
    Join Date
    Jan 2010
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP int weired behavior

    I'm building a web form in php, and while at testing the code, it is not showing the correct way, that I wanna.

    I mean to say

    It is showing integer value as false, as an example
    PHP Code:
    $int 011;
    $int2 = !is_int($int);
    echo 
    $int2
    it is showing this result as 1, but only when I enter the value through form with post method.

    While if I test the same in another script without post and form processing then it is working fine.

    So, I wanna know why it is showing such weired behavior ?

    Here is the full code of my form:

    PHP Code:
    <?php
    require_once "includes/functions.php";
    require_once 
    "database.php";
    if(!
    loggedin())
    {
        
    header("Location: login.php");
    }
    if(
    $_GET == false)
    {
        
    $user $_SESSION['NORMAL_USER'];
        
    $uid $_SESSION['USER_ID'];
        echo 
    $uid;
        echo 
    "<br />";
    }


    if(
    $_POST)
    {
        
        if(
    htmlCheck($_POST))
        {
            echo 
    "<meta http-equiv=refresh content=4>";
            
    $message htmlCheck($_POST);
        }
        else
        {
            
    $expected = array('PAN''mobile''std''fone''father''day''month''year''flat''area''town''state''PIN');
            
            
    $required = array('PAN''mobile''std''fone''father''day''month''year''flat''area''town''state''PIN');
            
            
    $missing = array();
            
    $error = array();
                
            foreach(
    $_POST as $key => $value
                {
                    
    $temp is_array($value) ? $value trim($value);
                    
    //if empty and required, add to $missing array
                        
    if(empty($temp) && in_array($key$required)) 
                        {
                            
    array_push($missing$key);
                        } 
    // otherwise, assign to a variable of the same name as $key
                        
    elseif(in_array($key$expected))
                        {
                            
    $temp mysql_real_escape_string(htmlentities(strip_tags($temp)));
                            ${
    $key} = $temp;
                        }
                }
        echo 
    "<pre>";
        
    print_r($_POST);
        
    print_r($missing);
        echo 
    "</pre>";
        echo 
    "<hr />";
        echo 
    "$std";
        echo 
    "<br />$fone<br />";
        
    $int = !is_int($std);
        echo 
    $int."<br />";
        
    $int1 = !is_int($fone);
        echo 
    $int1."<br />";
        
    $int3 = !is_int($std) || !is_int($fone);
        echo 
    $int3."<br />";
        
            if(empty(
    $missing))
            {
                if(
    strlen($PAN) != 10)
                {
                    
    $message "You have not entered the correct value for PAN Number. Please rectify it.";
                    
    //array_push($error, $message);
                
    }
                elseif(!
    is_numeric($mobile) || strlen($mobile) != 10)
                {
                    
    $message "You have not entered the correct Mobile No.";
                }
                elseif(!
    is_int($std) || !is_int($fone))
                {
                    
    $message "You have not entered the correct STD or Phone No.";
                }
                elseif(!
    filter_var($fatherFILTER_SANITIZE_STRING))
                {
                    
    $message "You have not entered the ";
                }
                elseif(!
    is_int($day) || !is_int($month) || !is_int($year))
                {
                    
    $message "You have not entered the correct value for your Date of Birth";
                }
                elseif(!
    filter_var($flatFILTER_SANITIZE_STRING))
                {
                    
    $message "You have not entered the correct value for the flat";
                }
                elseif(!
    filter_var($areaFILTER_SANITIZE_STRING))
                {
                    
    $message "You have not entered the correct value for the area";
                }
                elseif(!
    filter_var($townFILTER_SANITIZE_STRING))
                {
                    
    $message "You have not entered the correct value for the town";
                }
                elseif(!
    filter_var($stateFILTER_SANITIZE_STRING))
                {
                    
    $message "You have not entered the correct value for the state";
                }
                elseif(!
    is_int($PIN) && strlen($PIN) != 6)
                {
                    
    $message "Please provide the correct PIN CODE of your area which should be of 6 digit only.";
                }
                else
                {
                    
    $date "$year-$month-$day";
                    
    $phone "$std-$fone";
                    
    $sql "insert into userinfo(uid, PAN, DOB, mobile, phone, father, flat, area, town, state, pin) ";
                    
    $sql .= "values('$uid', '$PAN', '$date', '$mobile', '$phone', '$father', '$flat', '$area', '$town', '$state', '$PIN')";
                    
    $res query($sql);
                    if(
    $res)
                    {
                        
    $message "You have successfully entered the values. Now we will contact you soon if further information would be needed.";
                        
    $tableid mysql_insert_id();
                        echo 
    $tableid;
                    }
                }
                
            }
            else
            {
                
    $message "Please fill all the fields.";
            }
                    
        }
    }



    include_once 
    "layout/header.php";
    if(
    $user) echo "Welcome "ucfirst($user).", this is your first time here on this website, so please have a look below form and fill it so that it will be helpful for us to file your return.";

    ?>
    <?php  
    if(isset($message)) echo "<p>".$message."</p><br />"?>
    <form action="" method="post" class="update">
        <fieldset class="update">
        <legend class="update">General Information</legend>
            <table class="update">
                <tr>
                    <td><label class="update" for="PAN">PAN Card Number: </label></td>
                    <td><input type="text" name="PAN" title="Provide Your PAN Number" size="15" maxlength="12" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="mobile">Mobile Number: </label></td>
                    <td><input type="text" name="mobile" title="Provide Your Mobile Number" size="15" maxlength="10" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="std">Phone Number </label></td>
                    <td><input type="text" name="std" title="STD code:" maxlength="5" size="5" />
                        <input type="text" name="fone" title="Phone number" size="10" maxlength="10" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="father">Father's Name: </label></td>
                    <td><input type="text" name="father" title="Your Father's Name:" size="21" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="DOB">Date of Birth: </label></td>
                    <td>
                      <select name="day">
                          <option>Day</option>
                          <?php
                          
    for($i 1$i<= 31$i++)
                          {
                              echo 
    "<option value='$i'>$i</option>";
                          }
                          
    ?>
                      </select>
                      
                      <select name="month">
                          <option>Month</option>
                          <?php
                          
    for($i1$i <= 12$i++)
                          {
                              echo 
    "<option value='$i'>$i</option>";
                          }
                          
    ?>
                      </select>
                      
                      <select name="year">
                          <option>Year</option>
                          <?php
                          
    for($i 1940$i <= 2000$i++)
                          {
                              echo 
    "<option value='$i'>$i</option>";
                          }
                          
    ?>
                      </select>
                     </td>
                </tr>
             </table>
        </fieldset>
        
        <fieldset class="update">
        <legend class="update">Address Information</legend>
            <table class="update">
                <tr>
                    <td><label class="update" for="flat">Flat/Door/Block No.: </label></td>
                    <td><input type="text" name="flat" title="Your Flat/Door No." /></td>
                </tr>
                <tr>
                    <td><label class="update" for="area">Area Locality: </label></td>
                    <td><input type="text" name="area" title="Your Area or Locality" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="town">Town/City/District: </label></td>
                    <td><input type="text" name="town" title="Your Town or City or District" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="state">Your State: </label></td>
                    <td><input type="text" name="state" title="Your State Name" /></td>
                </tr>
                <tr>
                    <td><label class="update" for="PIN">PIN CODE: </label></td>
                    <td><input type="text" name="PIN" title="PIN CODE of your area." size="10" maxlength="6" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" title="Please check before submitting" size="10" value="Submit" /></td>
                </tr>
             </table>
        </fieldset>
    </form>

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is $int2? The name makes no sense and I promise you will hit yourself for using that name in a few weeks.

    Anyway, the problem is you have a 0 before the number. 11 is an int, 011 is not.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Actually a zero prefix sets it as an Octal integer value.
    PHP Code:
    $a 1234// decimal number
    $a = -123// a negative number
    $a 0123// octal number (equivalent to 83 decimal)
    $a 0x1A// hexadecimal number (equivalent to 26 decimal)

    /*
    decimal     : [1-9][0-9]*
                | 0

    hexadecimal : 0[xX][0-9a-fA-F]+

    octal       : 0[0-7]+

    integer     : [+-]?decimal
                | [+-]?hexadecimal
                | [+-]?octal
    */ 
    When passing values though a form they all come back as strings.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Zealot cools_sonu's Avatar
    Join Date
    Jan 2010
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AlienDev View Post
    What is $int2? The name makes no sense and I promise you will hit yourself for using that name in a few weeks.

    Anyway, the problem is you have a 0 before the number. 11 is an int, 011 is not.
    $int2 is for the sake of example only.

    while in the form I've used the proper name, well yeah U are right, names without proper sense are really hard to track down.

    Now back to the my question.

    As I said if I'm sending the information through POST, then it is showing the value as false, but if I'm testing the same thing in a separate script, then it is showing as true.

    And there I'm struck.

    While I can shift to is_numeric way, but I want to know why such behavior, and the values are simple integer, so I want to avoid any float or double values.

    And also is_int is showing 10 digit value as false integer. I'm clueless here.

  5. #5
    SitePoint Zealot cools_sonu's Avatar
    Join Date
    Jan 2010
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Actually a zero prefix sets it as an Octal integer value.
    PHP Code:
    $a 1234// decimal number
    $a = -123// a negative number
    $a 0123// octal number (equivalent to 83 decimal)
    $a 0x1A// hexadecimal number (equivalent to 26 decimal)

    /*
    decimal     : [1-9][0-9]*
                | 0

    hexadecimal : 0[xX][0-9a-fA-F]+

    octal       : 0[0-7]+

    integer     : [+-]?decimal
                | [+-]?hexadecimal
                | [+-]?octal
    */ 
    When passing values though a form they all come back as strings.
    hmmm

    then should I avoid using is_int ?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is_int() checks for the php data type int. A numeric looking string is not an integer. See ctype_digit()

    You can read more about types
    http://www.php.net/types

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is_int does not check to see if the variable contains an integer. Rather, it checks to see whether the data type of the variable is an integer.

    See:
    http://php.net/manual/en/function.ctype-digit.php

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    You can either cast to an integer or use is_numeric() instead.

  9. #9
    SitePoint Zealot cools_sonu's Avatar
    Join Date
    Jan 2010
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    You can either cast to an integer or use is_numeric() instead.
    I'm thinking of using is_numeric.

    but one concern about is_numeric is that it will open the door for floats and doubles too. Which I don't wanna. as it will cause problem lateron.

    As mobile no. can't be floating numbers.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...use ctype_digit().

    Also, you may instead want to use a free-form textbox and then do some intelligent detection. People like to input data in forms in different ways, and you should accept some variability. Some people might add spaces, for example.


Bookmarks

Posting Permissions

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