[Solved] PHP’s typeof – gettype()

0 min read 173 words

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.

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags