Checking To See If Something Is Not An Integer

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Checking To See If Something Is Not An Integer

ginginca
I put together this:

CODE

//Make Sure $qty is NOT blank before checking if it is a number
    if (!empty($item_1_qty)) {
//not empty        
    }
    if (intval($item_1_qty) !== $item_1_qty){
            //item 1 qty is not a whole number
            $redirect_to = 'order_form.php?error=6';    
            header("Location: $redirect_to");
            exit();
        }


But I get my error message coming no matter whether it is an integer or not.

 

 

 


Reply

jlhaslip
QUOTE
if (intval($item_1_qty) !== $item_1_qty){


Where is the value for the $item_1_qty coming from? if it is coming from a form, it may be cast as a string and the php.net site lists a series of examples which you might be interested in reviewing.
http://us3.php.net/manual/en/function.intval.php

Reply

TavoxPeru
QUOTE(ginginca @ Nov 19 2006, 07:16 PM) *

I put together this:

CODE

//Make Sure $qty is NOT blank before checking if it is a number
    if (!empty($item_1_qty)) {
//not empty        
    }
    if (intval($item_1_qty) !== $item_1_qty){
            //item 1 qty is not a whole number
            $redirect_to = 'order_form.php?error=6';    
            header("Location: $redirect_to");
            exit();
        }


But I get my error message coming no matter whether it is an integer or not.

If your variable is coming from a form i reccomend you to cast it before you use it, its simple:
CODE
$item_1_qty = (int) $_POST['item_1_qty'];

Also take a look to the is_numeric() php function, because it is recommended to use it with variables that are submited by a form.

Best regards,

 

 

 


Reply

Hercco
PHP has a function called is_int() to test if parameter is integer. Why not just use that?

Reply

Quatrux
..and why not better to use a function is_numeric() to see if the variable is a number or a numeric string, or you really need it to be integer.. But if so, the above replies have said enough. wink.gif

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

Recent Queries:-
  1. php check if something is an integer - 306.04 hr back. (1)
Similar Topics

Keywords : checking, integer

  1. Type Checking Not Work On For .. In Loop
    Flash 8 - actionscript 2.0 (2)
  2. Checking Without Loading
    (1)
    i like how some sites check the username you inputed and tells you if its not available without
    loading. Like hotmail when trying to make an email. How do you do that?....
  3. Checking For Plane Intersection.
    (3)
    Hello everyone. I am new to C++ game programming but I know so much already. I use 3D game
    programming in C++ at the moment. I was just wondering what the easiest way to check if two objects
    intersect in planes. Lets say I have two cubes with the sides of 5. My GL would be based off of
    float var x. Every vertex would be (x,x,x),(x,+5,x) est. Now to keep it simple I will assume that
    they are 2D not 3D. Here is an example of what my vertexes might be: 1 (front only) 2 (front only)
    (0,5)(5,5) (-5,5)(0,5) (0,0)(5,0) (-5,0)(0,0) Up until now I have been running a long IF c....
  4. The Sum Of The Cubes Of The Digits Of Any Non-negative Integer
    (5)
    Ok I need someone to help me tackle this program. Here's the question: Exercise 9 a) Write a
    program that displays the sum of the cubes of the digits of any non-negative integer. Two program
    runs are shown below: Enter an integer: 145 Sum of digits is 10
    Enter an integer: 8888 Sum of the cubes of the digits is 2048 /cool.gif"
    style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> Modify the program to
    determine what integers of two, three, or four digits are equal to the sum of the cubes of their
    digit....
  5. Data Convertion
    the result of converting to integer is undefined? (0)
    If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), the result of
    converting to integer is undefined? CODE unction StrToNum($Str, $Check, $Magic) {
        $Int32Unit = 4294967296;  // 2^32     $length = strlen($Str);     for ($i = 0; $i
            $Check *= $Magic;              if (is_float($Check)) {             $Check = ($Check -
    $Int32Unit * (int) ($Check / $Int32Unit));             $Check = ($Check         }         $Check +=
    ord($Str{$i});     }     return $Check; } refer to
    http://www.php.net/manual/en/language.types.integer.....
  6. A Simple Checking & Validation PHP Script
    (6)
    Hi, there is sometimes that you need to password protect a directory in your site but you dont have
    access to a database or you dont need it because only a few users will access this directory, well
    the following script i develop will help in this situation. With only 2 files you can implement a
    basic security, the first file is a simple txt file where you store your users information and the
    second file is the php script. You can name the files whatever you want and can be used in any site
    with php support. The users.txt file: In this file simply put one line at the....
  7. How Do I Do Script Checking & Validation In PHP?
    (17)
    I'm totally new to all this, and I've just done a registration/login script. It has 5
    different files that I put stuff in, plus a database. When I try to see how it looks on my website,
    well, it just doesn't show up. What I want to know is, how can I tell what I'm doing wrong?
    I don't always understand what the editors in the script programs are trying to say. Is there
    anyone out there who can help? My scripts are written in PHP with a MySql database. Boy, this all
    gets confusing, but I sure love every minute of it!....
  8. A Small Project: Splitting An Integer Into Digits
    (5)
    I need to know how to "split" a number up into seperae numbers, using VB. For example, "54321". I
    need to split that into 5 seperate numbers - "5," "4," "3," "2," "1," and assign them to a variable,
    if possible. This next part isn't important, but if you're curious why I need to know ...
    Eventually what I need to do is split the number into these individual parts and add them together.
    With that new number I need to again split it and take the last number subtracted from 10. That new
    number will then be placed at the end of the beginning number. Example: 5+4+3....
  9. Php: Want Script Checking/correcting Software
    (3)
    Hello Friends i wanted To know if Any PHP Correctiong softwares are available or not.? What i mean
    is ,Like If i have a Page with error on line 25 , And Is there any software which can scan and
    Correct the Php errors? Regards Casanova !....
  10. Php Script Testing/checking
    (6)
    I have two scripts I would like checked one is a server status script CODE $l_ip =
    'xx.xxx.xxx.26'; //Login IP $l_port = '6901'; //Login Port $c_ip =
    'xx.xxx.xxx.68'; //Char IP $c_port = '6121'; //Char Port $m_ip =
    'xx.xxx.xxx.68'; //Map IP $m_port = '5121'; //Map Port $checktime = '.3';
    //How long to check each server for before determining that it's offline //Check Login-Server
    if ( @fsockopen( $l_ip, $l_port, $errno, $errstr, $checktime) ) { echo "Login: Online "; } else {
    echo "Login: Offline ....

    1. Looking for checking, integer






*SIMILAR VIDEOS*
Searching Video's for checking, integer
advertisement




Checking To See If Something Is Not An Integer