julio 16, 2015

cachar $_POST como entero PHP



    When you extract the number value directly from $_POST, its data type is a string.
    You could use is_numeric to check:

    Code:

    if (is_numeric($theNum)) {
        echo $theNum . " is a number";
    } else {
        echo $theNum . " is not a number";
    }

    or a regular expression

    Code:

    if (preg_match('/^\d+$/', $theNum)) {
        echo $theNum . " is a number";
    } else {
        echo $theNum . " is not a number";
    }

    If you want to cast to an integer data type:

    Code:

    $theNum = (int)$_POST['thenum'];
    echo is_int($theNum) ? 'Number' : 'Not'; //Number

    http://www.webdeveloper.com/forum/showthread.php?255676-is_int%28%29-not-working-returning-false

No hay comentarios:

Se ha producido un error en este gadget.