compare the type of two variables in php

To compare the type of two variables in PHP, you can use the gettype() function to determine the type of each variable, and then compare those types using the === operator.

For example:

main.php
$var1 = 42;
$var2 = "42";

if (gettype($var1) === gettype($var2)) {
    echo "The types of \$var1 and \$var2 are the same.";
} else {
    echo "The types of \$var1 and \$var2 are different.";
}
194 chars
9 lines

In this example, the gettype() function is used to determine the type of each variable (integer and string, respectively), and then the === operator is used to compare the two types. Since the types are different, the else block is executed and the message "The types of $var1 and $var2 are different." is printed.

gistlibby LogSnag