试图检查PHP中的整数

The function below is trying to find whole numbers. But somehow I'm not doing this right as it shows every number to be whole (no digits after .)

$max_value % 1 should only return true is there is no rest value??

Can anyone help me, now every check gives 0 as result.

Hope I explain well what I mean

solution(30);

function solution($a){

 $sides_short = array();
 $sides_long  = array();
 $max_number  = $a/2;
 $total = $a;

 for($i=1;$i<=$max_number;$i++){
    $min_value = $i;
    $max_value = $a / $min_value;
    echo "loop values: ".$min_value." x ".$max_value."<br>";

    if($max_value % 1 == 0) {
        echo ($max_value % 1) ."<br>";
        echo "Aproved: (".$min_value." x ".$max_value.") $max_value % 1 = ".($max_value % 1)."<br><br>";
        $sides_short[] = $min_value; 
        $sides_long[] = $max_value;
    }
}

}

You're using a modulo. That divides by that number and returns the remainder.

$x = 3 % 2; // $x = 1

3 goes into 2 once, and leaves a remainder of 1

Anything divided by 1 is that number, with a remainder of 0. Therefore, this statement is always true.

if($max_value % 1 == 0)

Also, if you modulus a floating number, it will also be 0

var_dump(8 % 1.5); // int(0)

If you really need to modulus a float, use fmod

var_dump(fmod(8, 1.5)); // float(0.5)

The modulo operator % is not defined for floating point numbers, so you can not use modulo here. You can cast the float to an int and check, if both, the float and the int are equal. They can only be equal if the float was a number without any decimal places, as the cast to int would strip any decimal places and therefore make them unequal if there were any in the float.

$float = 1.5;
var_dump($float == intval($float));
$float = 1.0;
var_dump($float == intval($float));

Produces the output:

bool(false)
bool(true)