How can you verify the variable by one digit of the decimalization system?



  • There's something shorter than that.

    if ($var == 1 || $var == 2 || $var == 3 || $var == 4 || $var == 5 || $var == 6 || $var == 7 || $var == 8 || $var == 9 || $var == 0)
    

    ?



  • if (preg_match('!^[0-9]$!',$var))
    

    Other proposed options are not appropriate, including the author ' s code

    $var = "3 piglets";
    

    var_dump($var == 1 || $var == 2 || $var == 3 || $var == 4 || $var == 5 || $var == 6 || $var == 7 || $var == 8 || $var == 9 || $var == 0);
    var_dump(in_array($var, [1,2,3,4,5,6,7,8,9,0]));
    var_dump($var>=0 && $var<=9);
    var_dump(preg_match('!^[0-9]$!',$var));

    // bool(true)
    // bool(true)
    // bool(true)
    // int(0)

    PRI class, read about http://php.net/manual/ru/language.types.type-juggling.php ♪

    The great feature of the Stack Overflow websites is that most visitors are very familiar with the subject.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2