Page 1 of 1

Geting problem with is_integer function

Posted: Sat Jan 06, 2007 2:55 am
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>

Posted: Sat Jan 06, 2007 3:56 am
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']))

Posted: Sat Jan 06, 2007 4:00 am
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.

Posted: Sat Jan 06, 2007 9:32 am
by feyd

Posted: Sat Jan 06, 2007 9:43 am
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().

Posted: Sat Jan 06, 2007 10:41 am
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

Posted: Sat Jan 06, 2007 2:35 pm
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

Posted: Sat Jan 06, 2007 2:45 pm
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,
  ),
)

Posted: Sat Jan 06, 2007 2:55 pm
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

Posted: Sat Jan 06, 2007 2:58 pm
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

Posted: Sat Jan 06, 2007 3:02 pm
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.

Posted: Sat Jan 06, 2007 3:26 pm
by ms_dos10
thanx it's fine now