【问题标题】:Is it possible to differentiate whether an argument is a string or number? in PHP是否可以区分参数是字符串还是数字?在 PHP 中
【发布时间】:2013-12-02 04:44:13
【问题描述】:

假设这个

function Args($a) {
  if (/*HOW*/) {
    echo "the argument is a pure number";
  } else {
    echo "the argument is a string";
  }
}

Args(4); -> the argument is a pure number
Args("4"); -> the argument is a string

根据函数和示例,如何根据类型参数获得差异?

【问题讨论】:

    标签: php string types numbers arguments


    【解决方案1】:

    使用内置函数is_int($value)is_string($value)

    is_int 的手册是here, is_string 的手册here

    【讨论】:

    • 其实是is_integer。您还可以使用is_numeric 来确定字符串是否是可解析的;也就是说,如果它类似于"1.00"
    • @JimmySawczuk:is_integer实际上是函数is_intus2.php.net/manual/en/function.is-integer.php的别名
    【解决方案2】:

    使用内置的gettype() 函数。

    【讨论】:

      【解决方案3】:
      function Args($a) {
          $type=gettype($a);
          echo "the argument is a ".$type;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-01
        • 2018-11-08
        • 1970-01-01
        • 2019-04-29
        • 2011-08-07
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多