Page 1 sur 1

fonction is_int et is_float

Posté : 16 déc. 2010, 17:38
par Alpha_B747
2. La fontion is_int() fonctionne uniquement quand je passe une valeure en paramètre --> [ is_int(25) : fonctionne ] mais pas je pas je passe une variable ou une fonction qui rend une valeur --> [ is_int(sqrt($variable)) ] : ne fonctionne pas

Merci pour votre aide !

Re: fonction is_int et is_float

Posté : 16 déc. 2010, 17:57
par stealth35
qu'est ce que t'as en retour ? d'ailleurs sqrt renvoie un float

Re: fonction is_int et is_float

Posté : 17 déc. 2010, 07:42
par epommate2
Ah oui, tiens :

function another_is_int($a){
    return ((string) $a) === ((string)(int) $a);
}

echo "\nsqrt(2) : " . is_int(sqrt(2));
echo "\nsqrt(4) : " . is_int(sqrt(4));
echo "\nsqrt(2) : " . another_is_int(sqrt(2));
echo "\nsqrt(4) : " . another_is_int(sqrt(4));
Retour :

Code : Tout sélectionner

sqrt(2) : sqrt(4) : sqrt(2) : sqrt(4) : 1
Mais bon, c'est logique vu que la fonction sqrt http://www.php.net/manual/fr/function.sqrt.php renvoie un float (même si c'est un int).

Re: fonction is_int et is_float

Posté : 17 déc. 2010, 11:29
par stealth35
toujours faire un var_dump sur ce genre, false renvoie rien et true renvoie 1
function another_is_int($a){
    return ((string) $a) === ((string)(int) $a);
}

echo "sqrt(2) : " ; var_dump(is_int(sqrt(2)));
echo "sqrt(4) : " ; var_dump(is_int(sqrt(4)));
echo "sqrt(2) : " ; var_dump(another_is_int(sqrt(2)));
echo "sqrt(4) : " ; var_dump(another_is_int(sqrt(4)));

Code : Tout sélectionner

sqrt(2) : bool(false) sqrt(4) : bool(false) sqrt(2) : bool(false) sqrt(4) : bool(true)