Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:46:10+00:00 2026-06-17T15:46:10+00:00

How do I write a function that returns FALSE if a given string is

  • 0

How do I write a function that returns FALSE if a given string is
NOT A VALID NUMBER OF TYPE [PHP INT], and returns TRUE otherwise.

This is simple in other languages.

intval(), isint(), and is_numeric() are not adequate, here is why:

is_numeric() is not adequate because it matches to any number not just integers, also it accepts huge numbers as numeric, which aren’t integers. intval() is not adequate because it returns 0 for BOTH invalid PHP integers like ‘9000000000000000’ AND the valid PHP integer ‘0’ or ‘0x0’ or ‘0000’ etc. isint() only tests if a variable is already of type int, it doesn’t deal with strings or conversion to int.

Maybe there is a popular library for this or something?

I want to call a function that is capable of detecting whether the form data someone posts is a valid php integer, for instance.

I want to call the function that does this: is_php_integer($str_test_input). What goes in the function?

<?php

$strInput = 'test' //function should return FALSE
$strInput = '' //function should return FALSE
$strInput = '9000000000000000'  //function should return FALSE since
                            //is not valid int in php
$strInput = '9000' //function should return TRUE since
                    //valid integer in php
$strInput = '-9000' // function should return TRUE
$strInput = '0x1A' // function should return TRUE
                    // since 0x1A = 26, a valid integer in php
$strInput = '0' // function should return TRUE, since
                    // 0 is a valid integer in php
$strInput = '0x0' // function should return TRUE, since
                    // 0x0 = 0 which is a valid integer in php
$strInput = '0000' // function should return TRUE, since
                    // 0000 = 0 which is a valid integer in php

function is_php_integer($strTestInput) {
    // what goes here?
    // ...
    // if string could be interpreted as php integer, return true
    // else, return false
}

if is_php_integer($strInput) {
    echo 'your integer plus one equals: '. (intval($strInput) + 1);
} else {
    echo 'your input string is not a valid php integer'
}

?>

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:46:12+00:00Added an answer on June 17, 2026 at 3:46 pm
    <?php
    
    $input = array(
        'test',
        '',
        '9000000000000000',
        '9000',
        '-9000',
        '0x1A',
        '0',
        '0x0',
        '0000'
    );
    
    function is_php_integer($strTestInput) {
        return filter_var( $strTestInput, FILTER_VALIDATE_INT, array('flags' => FILTER_FLAG_ALLOW_OCTAL | FILTER_FLAG_ALLOW_HEX));
    
    }
    
    foreach ( $input as $value ) {
        if (is_php_integer($value) !== FALSE) {
            echo 'your integer plus one equals: '. (intval( $value ) + 1) . PHP_EOL;
        } else {
            echo 'your input string is not a valid php integer' . PHP_EOL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a javascript function that returns HTML content as string given
I have to write a function, that returns true if a given list is
Given an unsorted array of numbers, write a function that returns true if array
I am trying to write a function that returns the index of the passed
For the following script, how can I write a function that returns all of
One of the exercises in TC++PL asks: Write a function that either returns a
I want to write a function that takes a list of numbers and returns
I wrote a simple function that checks if a given string contains a predefined
I 've to do the following exercise: Write a function that given two integers
Given constant integers x and t, write a function that takes no argument and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.