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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)