oujood.com

Déterminer si une variable est de type nombre décimal

La fonction is_float() permet de déterminer si une variable donnée est de type nombre décimale

chercher |

PHP cours tutorial

La fonction is_float()

(PHP 4, PHP 5)

La fonction is_float ( ) détermine si une variable est de type nombre décimal.

Syntaxe :

is_float ( $var )

 

L’argument $var est la variable à vérifier.

La fonction is_float ( ) vérifie si la variable donnée est de type nombre décimal.

La fonction is_float ( ) retourne TRUE si $var est un nombre décimal, FALSE sinon.

Note:

Pour tester si une variable est un nombre ou une chaîne numérique (comme les entrées de formulaire, qui sont toujours des chaînes), vous devez utiliser la fonction is_numeric().

Exemple 1 :

<?php

if (is_float(27.25)) {

    echo "is float\n";

} else {

    echo "is not float\n";

}

var_dump(is_float('abc'));

var_dump(is_float(23));

var_dump(is_float(23.5));

var_dump(is_float(1e7));  //Notation Scientifique

var_dump(is_float(true));

?>

 

Ce qui affiche :

is float

boolean false
boolean false
boolean true
boolean true
boolean false

 

Exemple 2 :

Un exemple d’utilisation de is_float() dans une fonction.

 

<?php

function verifieDecimal($val){

    if( is_float($val) || ( (float) $val > (int) $val || strlen($val) != strlen( (int) $val) ) && (int) $val != 0  )

            {//return true;

            echo 'vrai <br>';}

    else {//return false;

            echo 'faux <br>';

}}

?>

 

<?php

//Tests

 verifieDecimal('4.0');      

 verifieDecimal('2.1') ;    

 verifieDecimal(0)      ;

 verifieDecimal("0")   ;

 verifieDecimal(3.)     ;

verifieDecimal(13)     ;

verifieDecimal("12")  ;

verifieDecimal(3.53)  ;

?>



Cours precedent:
  La fonction is_callable()

    Fonctions de manipulation des variables en PHP

 Cours suivant:
La fonction is_int()   

Par carabde 20 Aout 2014

MARKETPOWER-IA
Logiciel d'I.A.
Logiciel d'I.A. 100% Français pour lancer son business en ligne

La méthode secrète likes sans fin sur Facebook

Livre numérique court pour comprendre la méthode secrète permettant d'obtenir des likes sans fin sur Facebook.

GAGNER DE L'ARGENT
GRÂCE À FACEBOOK 
N'A JAMAIS ÉTÉ SI FACILE


Consulter


1tpe