Geting problem with is_integer function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ms_dos10
Forum Commoner
Posts: 42
Joined: Tue Jul 25, 2006 8:10 am

Geting problem with is_integer function

Post by ms_dos10 »

Geting problem with is_integer function . am trying to check weather the a variable pin is integer or not ,if it's not integer display message , but in the both case weather the variable is integer or not message it display on the screen

Code: Select all

<?php
ob_start();
/*
session_start();
if(!isset($_SESSION['sessref']))
{
        session_destroy();
        header("location:admincp.php");
        exit;
} */
?>
<html>
<head>
<link href = "includes/admin.css" type = "text/css" rel = "stylesheet">
<title>
Add Student
</title>
</head>
<body valign="top">
<img src="images/l.jpg" width="1000" height="150" alt="" border=0>
<br/>
<br/>
<?php
if(isset($_POST['submit']))
{
if($_POST['name'] && $_POST['pws'] && $_FILES['image'] && $_POST['e_mail'] && $_POST['pin'])
{
// here what am talking about ****Start**********
        if(!is_integer($_POST['pin']))
        {
             echo "<table width=\"600\" align=\"center\" border=\"0\">";
             echo "<tr>\n";
             echo "<td><font color=\"red\">*</font>Please Enter Pin Code As Number Not String</td><br/><br/>";
             echo "<tr>\n";
             echo "</table>\n";
             exit();
        }
//*********************End*******************
           include('includes/config.php');
           $spath = $_FILES['image']['tmp_name'];
           $dpath = "images/".$_FILES['image']['name'];
           move_uploaded_file($spath,$dpath);

          $insert = "insert into tbl_student values('','".$_POST['name']."','".$_POST['pws']."','".$dpath."','".$_POST['e_mail']."','".$_POST['pin']."')";
          $result = mysql_query($insert) or die(mysql_error());

          if(!$result)
          {
                  echo "Insert not successful!";
                  exit;
          }
         header("location:student_list.php");
         exit;
}
else if($_POST['name'] || $_POST['pws'] || $_FILES['image'] || $_POST['e_mail'] || $_POST['pin'])
{
        echo "<table width=\"600\" align=\"center\" border=\"0\">";
        echo "<tr align=\"center\">\n";
        echo "<td><font color=\"red\">*</font>Please Fill all field</td><br/><br/>";
        echo "<tr>\n";
        echo "</table>\n";
}
}

ob_end_flush();
?>
<table width="600" align="center" border="1">
<form name="insert" method="post" action="" enctype="multipart/form-data">
<tr>
    <th colspan="2" align="center">Add Student</th>
</tr>
<tr>
    <td align="right">Name:</td><td align="left"><input type="text" name="name"/></td>
</tr>
<tr>
    <td align="right">Password:</td><td align="left"><input type="password" name="pws"/></td>
</tr>

<tr>
    <td align="right">image:</td><td align="left"><input type="file" name="image"/></td>
</tr>
<tr>
    <td align="right">E_mail:</td><td align ="left"><input type="text" name="e_mail"/></td>
</tr>
<tr>
    <td align="right">Pin_code:</td><td align="left"><input type="text" name="pin"/></td>
</tr>

<tr>
    <td align="right"><input type="submit" name="submit" value="Add"/></td>
    <td align="left"><input type="reset" name="reset" value="Clear"/></td>
</tr>
</form>
</table>
</body>
</html>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I get the impression that is_int() thinks your post value is a string.

is_numeric() is too generous, including floats, etc...

this works:

Code: Select all

if(!preg_match('/^\d+$/',$_POST['pin']))
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Another thing:

Code: Select all

if($_POST['name'] || $_POST['pws'] || $_FILES['image'] || $_POST['e_mail'] || $_POST['pin'])
This checks:
if name is posted OR pws is posted OR files[images] exist OR .. etc etc

You probably want something like:

Code: Select all

<?php
if ( empty($_POST['name']) || empty($_POST['pws']) || .... etc ) 
{
    // one or more are empty ..
}
?>
A foreach loop to check which ones are missing and then returning specific error messages is probably what you want.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

matthijs wrote:Another thing:

Code: Select all

if($_POST['name'] || $_POST['pws'] || $_FILES['image'] || $_POST['e_mail'] || $_POST['pin'])
This checks:
if name is posted OR pws is posted OR files[images] exist OR .. etc etc
In this context, it is checking whether it is true or false. And in this context, if the FILES array is empty, that var will trigger an undefined index warning. You should be checking isset() or empty() before blindly pushing vars into your conditionals, as matthijs suggested.

Come to think of it, that entire conditional will fire undefined index warnings if those array indexes aren't set, so each of those should be checked against isset() or empty().
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Everah makes some good suggestions. It's worthwhile to study those functions (and related ones) very well, because there are some subtle but important differences between them (when each function returns true or false, etc)

Here's a short article http://www.nyphp.org/phundamentals/vari ... uation.php
ms_dos10
Forum Commoner
Posts: 42
Joined: Tue Jul 25, 2006 8:10 am

Post by ms_dos10 »

thanx all . i wanna say i was trying to check weather the variable is integer (Number) variable or not but it's not working with me . i don't know what happen to is_integer() function . but this function ctype_digit() work good with me . am study very hard all the variable handling function , what i need to know ? why is_integer function not work in this case ?
and this link is good but more than good
http://www.nyphp.org/phundamentals/vari ... uation.php
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is_integer() checks the type PHP has it stored as internally, not what the content is. A string is not an integer in any way, shape or form.

Code: Select all

[feyd@home]>php -r "$test = array('1',1,true,'',0,null,false); $results = array('types' => array_map('gettype', $test), 'strings' => array_map('is_string', $test), 'integers' => array_map('is_int', $test), 'booleans' => array_map('is_bool', $test), 'scalars' => array_map('is_scalar', $test),); var_export($results);"
array (
  'types' =>
  array (
    0 => 'string',
    1 => 'integer',
    2 => 'boolean',
    3 => 'string',
    4 => 'integer',
    5 => 'NULL',
    6 => 'boolean',
  ),
  'strings' =>
  array (
    0 => true,
    1 => false,
    2 => false,
    3 => true,
    4 => false,
    5 => false,
    6 => false,
  ),
  'integers' =>
  array (
    0 => false,
    1 => true,
    2 => false,
    3 => false,
    4 => true,
    5 => false,
    6 => false,
  ),
  'booleans' =>
  array (
    0 => false,
    1 => false,
    2 => true,
    3 => false,
    4 => false,
    5 => false,
    6 => true,
  ),
  'scalars' =>
  array (
    0 => true,
    1 => true,
    2 => true,
    3 => true,
    4 => true,
    5 => false,
    6 => true,
  ),
)
ms_dos10
Forum Commoner
Posts: 42
Joined: Tue Jul 25, 2006 8:10 am

Post by ms_dos10 »

Sorry am not get anything from what you wrote can explain more to me , in easy way i don't have that much
ms_dos10
Forum Commoner
Posts: 42
Joined: Tue Jul 25, 2006 8:10 am

Post by ms_dos10 »

and what the different between the two function is_integer and ctype_digit function .
i think both they check if the variable is number or not or am wrong
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

is_integer() (or is_int()) checks whether the variable type is an integer, not a number. Read the manual on the is_int() function. There is a big note on the top of the page that tells you what you should do if you want to test if a variable is a number (hint: is_numeric()).

ctype_digit(), well, click on the link of the function name and read up on it. It is pretty self explanatory.
ms_dos10
Forum Commoner
Posts: 42
Joined: Tue Jul 25, 2006 8:10 am

Post by ms_dos10 »

thanx it's fine now
Post Reply