Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Checking To See If Something Is Not An Integer
ginginca
post Nov 20 2006, 12:16 AM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 250
Joined: 6-July 06
From: The net (or at least that's what my family says)
Member No.: 14,330



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.

This post has been edited by ginginca: Nov 20 2006, 12:16 AM
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 20 2006, 02:55 AM
Post #2


Advanced Member
Group Icon

Group: Members
Posts: 187
Joined: 15-November 05
From: Inland from the Left Coast of Canada
Member No.: 9,627



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
Go to the top of the page
 
+Quote Post
TavoxPeru
post Nov 20 2006, 10:08 AM
Post #3


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



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,
Go to the top of the page
 
+Quote Post
Hercco
post Nov 23 2006, 05:52 PM
Post #4


Super Member
Group Icon

Group: Members
Posts: 595
Joined: 4-September 04
Member No.: 228



PHP has a function called is_int() to test if parameter is integer. Why not just use that?
Go to the top of the page
 
+Quote Post
Quatrux
post Nov 23 2006, 06:54 PM
Post #5


the Q
Group Icon

Group: [HOSTED]
Posts: 1,054
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



..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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Type Checking Not Work On For .. In Loop(2)
  2. Checking Without Loading(1)
  3. Checking For Plane Intersection.(3)
  4. The Sum Of The Cubes Of The Digits Of Any Non-negative Integer(5)
  5. Data Convertion(0)
  6. A Simple Checking & Validation PHP Script(6)
  7. How Do I Do Script Checking & Validation In PHP?(17)
  8. A Small Project: Splitting An Integer Into Digits(5)
  9. Php: Want Script Checking/correcting Software(3)
  10. Php Script Testing/checking(6)


 



- Lo-Fi Version Time is now: 13th October 2008 - 09:23 PM