简体   繁体   中英

PHP code to check if array is numeric is not working

I have the following PHP:

 <?php

 $array = array("1","2","3");
 $only_integers === array_filter($array,'is_numeric'); // true

 if($only_integers == TRUE)
 {
 echo 'right';
 }

 ?>

For some reason it always returns nothing. I don't know what I'm doing wrong.

Thanks

is_int checks the actual type of a variable, which is string in your case. Use is_numeric for numeric values regardless of variable type.

Note that the following values are all considered "numeric":

"1"
1 
1.5
"1.5"
"0xf"
"1e4"

ie any floats, integers or strings that would be valid representations of floats or integers.

Edit: Also, you might have misunderstood array_filter , it does not return true or false but a new array with all values for which the callback function returned true. if($only_integers) works nonetheless (after you fixed your assignment operator) because all non-empty arrays are considered "true-ish".

Edit 2: as @SDC pointed out, you should use ctype_digit if you only want to allow integers in decimal format.

You have to compare the length of the original array to the length of the filtered array. The array_filter function returns an array with values matching the filter set to true.

http://php.net/array_filter

 if(count($only_integers) == count($array))  {
     echo 'right';
 } else {
     echo 'wrong';
 }
  1. is_int() will return false for "1" because it's a string.
    I see you've edited the question now to use is_numeric() instead; this is also probably a bad idea, as it will return true for hex and exponent values, which you probably don't want (eg is_numeric("dead") will return true).
    I suggest using ctype_digit() instead.

  2. The triple-equal is being misused here. It is used for a comparison, not an assignment, so $only_integers will never be set. Use single-equal to set $only_integers .

  3. array_filter() doesn't return a true / false value; it returns the array, with the filtered values removed. This means that the subsequent check that $only_integers is true will not work.

  4. $only_integers == TRUE . This is okay, but you probably should have used the triple-equal here. But of course, we already know that $only_integers won't be true or false , it'll be an array, so actually we need to check whether it's got any elements in it. count() would do the trick here.

Here's what your code looks like, taking all that into account...

 $array = array("1","2","3");
 $only_integers = array_filter($array,'ctype_digit'); // true

 if(count($only_integers) > 0)
 {
     echo 'right';
 }

change === with = it used to compare not for initialise a variable

<?php

 $array = array(1,2,3);
 $only_integers = array_filter($array,'is_int'); // true

 if($only_integers == TRUE)
 {
 echo 'right';
 }

?>

Do you try to run your code before posting ? I have this error :

Notice: Undefined variable: only_integers in ~/php/test.php on line 4
Notice: Undefined variable: only_integers in ~/php/test.php on line 6

Change === to = fixes the problem right away. You better learn how to use phplint and other tools to avoid typo mistake like this.

<?php
$test1 = "1";
if (is_int($test1) == TRUE) {
    echo '$test1 is an integer';
}
$test2 = 1;
if (is_int($test2) == TRUE) {
    echo '$test2 is an integer';
}
?>

try this code and you'll understand why your code doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM