0

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.

5
  • you can use functions like is_int(), is_float() or is_string() Commented Jun 23, 2017 at 15:41
  • if you have values="2015-02-17" and values="Hello" and i use if(is_string($values){ echo "have string" } else echo " have date" this don't work's because values is taken for string and not for date Commented Jun 23, 2017 at 15:42
  • Both "2015-02-17" and "Hello" are strings ... are you expecting something different? Technically even "10" would be a string as it's in " marks. Commented Jun 23, 2017 at 15:46
  • Yes,sorry for my title is incomplete. if i have values=Hello or values=2015-02-17 values=10, something changes? Commented Jun 23, 2017 at 15:48
  • The only way that "2015-02-17" would be a date is if you made it one in the first place with something like new DateTime('2015-02-17') (which would be a DateTime object) ... otherwise it is just a string. Commented Jun 23, 2017 at 15:54

3 Answers 3

1

You can try the php gettype() function to find the type

Sign up to request clarification or add additional context in comments.

3 Comments

My problem is that, I can not distinguish the date from the string.
you can try strtotime() function which will convert to a time stamp so that you can distinguish date and string
I used a shortcut with strtotime(), It's not nice but it works :). Thank you so much
0

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.

Comments

0

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

1 Comment

ok, but if you have values="10" with " " don't have different with string and numeric. With Value=10 without " " have only numeric

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.