简体   繁体   中英

How check if a variable is a date, string or float (numeric) [PHP]

Hy guys, i have a problem.

My variable can have different values in my cycle:

  • values="10"
  • values="Hello"
  • values="2015-02-17"

How do I check the type of variable with a condition? I have tried but string type is "equal" to date type.

您可以尝试使用php gettype()函数查找类型

There are a variety of built-in functions in PHP, mainly beginning with is_

  • is_a is_a — Checks if the object is of this class or has this class as one of its parents
  • is_array — Finds whether a variable is an array
  • is_binary — Finds whether a variable is a native binary string
  • is_bool — Finds out whether a variable is a boolean
  • is_buffer — Finds whether a variable is a native unicode or binary string
  • is_callable — Verify that the contents of a variable can be called as a function
  • is_dir — Tells whether the filename is a directory
  • is_double — Alias of is_float()
  • is_executable — Tells whether the filename is executable
  • is_file — Tells whether the filename is a regular file
  • is_finite — Finds whether a value is a legal finite number
  • is_infinite — Finds whether a value is infinite
  • is_int — Find whether the type of a variable is integer
  • is_link — Tells whether the filename is a symbolic link
  • is_nan — Finds whether a value is not a number
  • is_null — Finds whether a variable is NULL
  • is_numeric — Finds whether a variable is a number or a numeric string
  • is_object — Finds whether a variable is an object
  • is_readable — Tells whether a file exists and is readable
  • is_resource — Finds whether a variable is a resource
  • is_scalar — Finds whether a variable is a scalar

There are a few others - some are aliases of some mentioned here - others not. The PHP manual will offer far more information on each but they can be used to form the backbone of a logical test for your variables.

It's a bit hacky but you could so something like:

function dataType($data) {

    // is_numeric() will validate ints and floats
    if(is_numeric($data)) return "number";

    // if $data is not a valid date format strtotime() will return false
    // so we're just using it as a validator basically
    elseif(strtotime($data)) return "date";

    // is_string() does what you'd expect
    elseif(is_string($data)) return "string";

    // anything else that you're not looking for
    else return "other";

}

echo dataType("Hello"); // outputs "string"

There would be issues with is_numeric() however if your numbers use , separators in any way, for instance NL number formating 1,5 or UK formatting with thousand separators 1,000,000 .

You could get around this with the number_format() function if that's the case: http://php.net/manual/en/function.number-format.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM