duanjue7745 2018-10-02 06:51
浏览 41
已采纳

读取用户输入并检查数据类型

I've simple PHP script:

<?php
$input = readline();

echo gettype($input);
?>

It reads user input from the console. What I am trying to achieve is to get properly data type. At the moment $input is string type.

I need something like this:

Input    Output
 5       Integer
2.5      float
true     Boolean

I can't get any idea how to do it. Thanks.

EDIT: Thanks to @bcperth answer, I achieve this working code:

<?php
 while(true) {
 $input = readline();
 if($input == "END") return ;
  if(is_numeric($input)) {
      $sum = 0;
      $sum += $input;
       switch(gettype($sum)) {
           case "integer": $type = "integer"; break;
           case "double": $type = "floating point"; break;
       }
       echo "$input is $type type" . PHP_EOL;
  }
  if(strlen($input) == 1 && !is_numeric($input)) {
      echo "$input is character type" . PHP_EOL;
  } else if(strlen($input) > 1 && !is_numeric($input) && strtolower($input) != "true" && strtolower($input) != "false") {
      echo "$input is string type" . PHP_EOL;
  }  if(strtolower($input) == "true" || strtolower($input) == "false") {
      echo "$input is boolean type" . PHP_EOL;
  }
 }
?>

Also tried with filter_var, working well:

<?php
while(true) {
    $input = readline();
    if($input == "END") return;
      if(!empty($input)) {
        if(filter_var($input, FILTER_VALIDATE_INT) || filter_var($input, FILTER_VALIDATE_INT) === 0) {
        echo "$input is integer type" . PHP_EOL;
        } else if(filter_var($input, FILTER_VALIDATE_FLOAT) || filter_var($input, FILTER_VALIDATE_FLOAT) === 0.0) {
        echo "$input is floating point type" . PHP_EOL;
        } else if(filter_var($input, FILTER_VALIDATE_BOOLEAN) || strtolower($input) == "false") {
        echo "$input is boolean type" . PHP_EOL;
        } else if(strlen($input) == 1) {
        echo "$input is character type" . PHP_EOL;
        } else {
        echo "$input is string type" . PHP_EOL;
        }
      }
}

?>
  • 写回答

1条回答 默认 最新

  • douzi7711 2018-10-02 07:13
    关注

    You would need to employ a few strategies as below for simple types.

    1. test if numeric using is_numeric().
    2. if numeric then add it to zero and gettype() the result
    3. if not numeric then compare to "true" and "false"
    4. if not "true" or "false" then its a string

    Here is a working start that shows how to go about it.

    <?php
    $input = readline();
    
    if (is_numeric($input)){
        $sum =0;
        $sum += $input;
        echo gettype($sum);
    }
    else {
        if ($input== "true" or $input == "false"){
            echo "boolean";
        }
        else {
            echo "string";
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错