Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: ctype and int

  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts

    ctype and int

    Why does this code fail and take the ELSE branch...

    PHP Code:
        if (isset($_GET['comment']) && $_GET['comment']){
            
    // CommentID Found in URL.
            // Cast to Integer.
            
    $commentID = (int)$_GET['comment'];


            
    // Check CommentID Format.
            
    if ((ctype_digit($commentID)) && ($commentID 0)){
                
    // Valid CommentID
                // Continue processing...

            
    }else{
                
    // Invalid CommentID 

    Is it because an Integer can be negative and thus have a minus sign in it?

    Sincerely,


    Debbie

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    From php.net:



    Your integer WILL return false. If you look at the manual for the function it shows the parameter as a string:



    You are casting it to an integer and then passing it in as a parameter.

    Instead, use the is_numeric() function as it will take strings and integers and return true if either are numerical.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    From php.net:



    Your integer WILL return false. If you look at the manual for the function it shows the parameter as a string:



    You are casting it to an integer and then passing it in as a parameter.

    Instead, use the is_numeric() function as it will take strings and integers and return true if either are numerical.
    So even a positive Integer fails.

    Interesting.

    Guess I have been technically using this the wrong way, but since I use it with URL validation, I get by.


    I don't like isnumeric() because I only want to check for positive whole numbers (i.e. "natural" numbers).

    Sincerely,


    Debbie

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by doubledee View Post
    So even a positive Integer fails.

    Interesting.
    It doesn't fail, the function deliberately returns a false result because it only accepts a string. If it is not a string, it will return false. You could think of it like this:

    PHP Code:
    function ctype_digit($Var)
       {
       if (
    is_string($Var))
          {
          return (
    is_numeric($Var))? truefalse;
          }
       else
          {
          return 
    false
          
    }
       } 

    Quote Originally Posted by doubledee View Post
    Guess I have been technically using this the wrong way, but since I use it with URL validation, I get by.
    Thats the point, anything passed via the url / GET array IS a string. If you remove your line of code that casts it to an integer you should be ok

    Quote Originally Posted by doubledee View Post
    I don't like isnumeric() because I only want to check for positive whole numbers (i.e. "natural" numbers).
    So include a second clause in your if test (as your original code shows): ($Var > 0) - job done!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    doubledee (06-01-2014)

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,092
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    It doesn't fail, the function deliberately returns a false result because it only accepts a string. If it is not a string, it will return false. You could think of it like this:

    PHP Code:
    function ctype_digit($Var)
       {
       if (
    is_string($Var))
          {
          return (
    is_numeric($Var))? truefalse;
          }
       else
          {
          return 
    false
          
    }
       } 
    That is a good way to think about things.


    Quote Originally Posted by tangoforce View Post
    Thats the point, anything passed via the url / GET array IS a string. If you remove your line of code that casts it to an integer you should be ok
    Yeah, I saw that.


    Quote Originally Posted by tangoforce View Post
    So include a second clause in your if test (as your original code shows): ($Var > 0) - job done!

    So, for what I am trying to do, which is better...

    Option #1:
    PHP Code:
        if (isset($_GET['page']) && $_GET['page']){
            
    // Page in URL.
            
    $currPage $_GET['page'];

            
    // Check Page-Format.
            
    if ((ctype_digit($currPage)) && ($currPage 0)){
                
    // Valid Page.

            
    }else{
                
    // Invalid Page.

            
    }
        } 

    Option #2:
    PHP Code:
        if (isset($_GET['page']) && $_GET['page']){
            
    // Page in URL.
            
    $currPage $_GET['page'];

            
    // Check Page-Format.
            
    if ((isnumeric($currPage)) && ($currPage 0)){
                
    // Valid Page.

            
    }else{
                
    // Invalid Page.

            
    }
        } 
    Sincerely,


    Debbie

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 344 Times in 340 Posts
    Quote Originally Posted by doubledee View Post
    I don't like isnumeric() because I only want to check for positive whole numbers (i.e. "natural" numbers).
    PHP Code:
    filter_input(INPUT_GET"comment"FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]]); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Take off (int) and it wont turn it into an integer then, it will be a string, then you can do that.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube


  •  

    Posting Permissions

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