[Solved] PHP’s Typeof – Gettype()


Quite often you may need the ability to check what type of data type a variable is.

In most programming languages this is possible to do and is usually called something like typeof().

So what is typeof in php, or typeof php you ask? In PHP there is a simple function called gettype() and it’s so easy to use.

All you have to do is pass the variable in question to gettype() as the first argument and the type will be returned.

You can echo out the result or use it in a comparison later on.

An example of how to use gettype():

function foo($bar) {
    echo gettype($bar);
}

foo("stringVar");
//echo's "stringVar";

foo($arrayVar);
//echo's "array";

In the above script we create a function called foo which has 1 argument.
It can take any data type (thanks PHP) so we call it twice in the example, first with a string argument and secondly with an array argument.

Read the official documentation on gettype here if you want to know all the ins and outs.