Programming PHP PHP gettype() Function: Check data type by PHP get type

PHP gettype() Function: Check data type by PHP get type

How to check Datatype in PHP - php gettype()

The PHP gettype() function is a PHP function that is used to check the type of variables. This PHP gettype() function returns the data type of the variable value passed as an argument and returns a string which is a clear data type of the passed value, like integer, double, string, etc.

Syntax of PHP gettype() function to get type in PHP

The parameter “values” is the required argument in this function. It returns the type as a string. As a result, the return values can be one of the following values: “integer”, “double”, “string”, “boolean”, “array”, “object”, “resource”, “NULL”, and “unknown type”.

Taking $value = ‘Hello’ which is a string type

This code will give the output:

I am trying to demonstrate almost all the possible cases with the below examples. If I missed something please mention it. in this case, I am taking a variable $value which will be passed as an argument in the PHP gettype() function to check its data type.

Check String type in PHP (PHP get type of String value)

Taking $value = ‘1234’ which is a string type containing numbers

Code Explanation

$value = '1234';: This line assigns the value '1234' to a variable called $value. Enclosing the value in single quotes indicates that it’s a string of characters.

echo "'1234' Is data type - ".gettype($value);: This line prints out a message using the echo statement. The message for output has two parts:

  • '1234' Is data type -: This part is a fixed text that says “‘1234’ Is data type – “.
  • gettype($value): This part uses the PHP gettype() function to determine the data type of the value stored in the $value variable. In this case, since the value is a string, it will return “string” as output.

So, when you run this code, you will get the output:

The code is displaying a message that tells you the data type of the value stored in the $value variable, which is the string '1234'.

Similarly, we can check other data types using PHP get type function. Further, I will demonstrate checking of Integer,

Check Integer data type in PHP gettype() (PHP get type of Integer)

In this case, I have taken the same value as in the String type value. Though, the single quotes are not here, representing the data to be integer values instead of String data type. Let’s check in the example.

Taking $value = 1234 which is an integer type (numeric)

PHP gettype() for double data type

Now, I have taken the decimal/float values, which are called double data types. So the output of this type should be “double”.

Taking $value = 56.02 and 29.00 which are both float(double) types but one has value after decimal however the other has not.

Testing the boolean values using PHP gettype()

In this case, you can input either true or false. As these two are boolean values. For both inputs, the output will be the same i.e., boolean.

Taking $value = true which is a boolean type (lets test it with $value = false)

Using PHP get type function to check NULL

What is NULL type in PHP

In PHP, NULL is a special data type that represents a variable with no value. It indicates the absence of a value or the uninitialized state of a variable.

In other words, when a variable is assigned the value NULL, which means that the variable doesn’t hold any data.

Here’s a brief overview of how NULL is used in PHP:

  1. Uninitialized Variables: If you declare a variable without assigning it a value, it is automatically assigned the value NULL. For example:
  1. Explicit Assignment: You can also explicitly assign the value NULL to a variable:

Keep in mind that NULL is different from an empty string "" or the integer 0. Though, all three represent the absence of a specific value. Their significance varies depending on the context of their use.

Moreover, if you need details on data types in PHP, Read the post Constants, Comments, and Data types in PHP.

Checking NULL types by php gettype()

Taking $value = null which is a NULL type (can be $value = NULL)

Using PHP gettype() to check array type

In PHP, an array is a composite data type that can hold multiple values of different types in an associative manner. It is a powerful data structure that allows you to store and manipulate collections of data elements, such as numbers, strings, or even other arrays.

Arrays can be indexed numerically or with string keys. Making them suitable for creating lists, maps, and more complex data structures. Additionally, the array offers a flexible way for grouping related data and finds extensive use in tasks such as storing form data, database results, configuration settings, and more.

Let’s check the array type using PHP get type function.

Taking $value = [10, 20, 30] which is an array

Now is the time to check a Class type by PHP gettype()

A class is a blueprint that defines how to create objects. Furthermore, it encapsulates data (properties) and methods(functions). These data and methods work together to create reusable code. This code targets creating and managing specific types of objects with similar characteristics and behaviors.

Now, check the type of Class using PHP get type

Taking $value = new MyClass() which is an object

Here’s how you can check the type of a resource/file in PHP

Taking $value as a resource to text file text.txt in the same directory, in the Read mode.

Conclusion

Concluding with, PHP gettype() function is used to check the data type of a variable, returning a string like “integer,” “string,” “boolean,” etc. I tried to demonstrate through examples, that show how to use PHP get type to distinguish between different data types like strings, integers, floats, booleans, NULL, arrays, and objects. I hope this will enhance the accuracy and efficiency of PHP coding. If any questions, you may in the comments section or can email me.

One thought on “PHP gettype() Function: Check data type by PHP get type

  1. hi iam new to php i have a doubt, how to find the datatype of the user input in php can anyone explain please

Average
5 Based On 1

Leave a Reply