Is it possible to create a three-dimensional operator for this expression in PHP?

    function integer($str)
    {
        if(preg_match('/[^0-9]/', $str)) 
        { 
           return FALSE;
        }

        else
        {
           return $str;
        }
    }

Is it possible to create a ternary operator for this operator in PHP?

+3
source share
2 answers

Yes:

function integer($str) {
  return (preg_match('/[^0-9]/', $str) ? false : $str);
}
+5
source

You should use ctype_digitfor this:

function integer($str) {
    return ctype_digit($str) ? $str : false;
}

Or use filter_varwithFILTER_VALIDATE_INT

function integer($str) {
    return filter_var($str, FILTER_VALIDATE_INT, array(
        'options' => array('min_range' => 0),
        'flags' => FILTER_FLAG_ALLOW_OCTAL
    ));
}
+3
source

Source: https://habr.com/ru/post/1789561/


All Articles