php

How to Use gettype() Function in PHP

The gettype() function in PHP is one of the most important functions that provide information about a variable’s data type. It is widely used in programming to find out the data type of a variable, which can help in determining the correct processing that the variable requires. This function is easy to apply and flexible and provides the necessary information about different data types.

Any PHP script can utilize this inbuilt function. The function outputs a string indicating the data type of the parameterized variable. For example, if the parameter passed is an Integer, the function will return “integer“.

Syntax of the gettype() Function

The syntax of the gettype() function is simple and requires only one parameter. The general syntax is:

gettype(variable)

 

Where the variable is the argument that is supplied to the function. The variable can be of any type, including strings, integers, floats, Booleans, or arrays.

How to Use gettype() Function in PHP?

You can use the gettype() function in PHP:

1: To Determine the Data Type

The following example shows the use of the gettype() function to determine the data type of different variables:

<?php
$integer = 32;
$string = "LinuxHint!";
$float = 2.43;
$boolean = false;
$array = array("english", "urdu", "maths");
echo gettype($integer), "\n";
echo gettype($string), "\n";
echo gettype($float), "\n";
echo gettype($boolean), "\n";
echo gettype($array), "\n";
?>

 

As shown in the example code, the gettype() function returns the data type of different variables. For example, the variable $integer has a data type of integer, the variable $string has a data type of string, and so on.

2: To Perform Conditional Actions

The gettype() function can also be helpful in performing conditional actions. For instance, we can use this function to check if a variable is of a particular data type before we perform an operation. We may show the user an error message if the data type is incorrect.

<?php
$age="3";
  if(gettype($age) != "integer") {
    echo "Error: Age must be an integer value.";
  }
?>

 

In this above code, the text value “3” is used to initialize the variable $age. The data type of $age is then established using the gettype() method, which is a string.

The if statement’s code block will run since the conditional statement will evaluate to true because the data type of $age is not an integer. The echo statement in the code block says, “Error: Age must be an integer value.”

3: To Perform Automatic Data Type Conversion

In addition to conditional actions, we can also use the gettype() function to perform automatic data type conversion. Suppose we have a string variable that contains an integer value, and we want to use it in an arithmetic operation. In that case, we can use the gettype() function to determine the data type of the variable, convert it to the correct data type, and perform the operation.

<?php
$numStr = "433";
if(gettype($numStr) == "string") {
  $num = (int) $numStr;
  $result = $num * 2;
  echo "Result: " . $result;
}
?>

 

In the above code, the string variable $numStr has the value “433” in it. The data type of $numStr is determined using the gettype() method to be a string. The string value is then typecast into an integer using the $num variable as a storage location.

After that, we multiply the $num variable by 2 and put the outcome in the $result variable to execute an arithmetic operation. Finally, the echo statement is used to show the result on the screen.

Conclusion

PHP’s gettype() function is essential for assisting developers in determining the data type of a variable. Apart from this, it can also help in automatic data type conversion, and performing conditional actions. This guide will help users understand the basic working of gettype() function with the help of some simple examples.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.