In PHP, you can use the gettype()
function to determine the data type of a value. This function returns a string that represents the data type of the value passed to it. For example:
$num = 10;
$str = "hello";
$bool = true;
$arr = array(1, 2, 3);
echo gettype($num); // integer
echo gettype($str); // string
echo gettype($bool); // boolean
echo gettype($arr); // array
In this example, the gettype()
function will return integer
for the $num
variable, string
for the $str
variable, boolean
for the $bool
variable, and array
for the $arr
variable.
You can also use the is_*
functions to check the data type of a value. For example, you can use is_int()
to check if a value is an integer, is_string()
to check if a value is a string, and so on. For example:
$num = 10;
$str = "hello";
$bool = true;
$arr = array(1, 2, 3);
if (is_int($num)) {
echo "num is an integer";
}
if (is_string($str)) {
echo "str is a string";
}
if (is_bool($bool)) {
echo "bool is a boolean";
}
if (is_array($arr)) {
echo "arr is an array";
}
This will output “num is an integer”, “str is a string”, “bool is a boolean”, and “arr is an array”.
You can also use the instanceof
operator to check if an object is an instance of a particular class. For example:
class MyClass {
}
$obj = new MyClass();
if ($obj instanceof MyClass) {
echo "obj is an instance of MyClass";
}
This will output “obj is an instance of MyClass”.