How to check Data type in PHP – PHP gettype()

How to check Data type in PHP – PHP gettype()

In PHP, the gettype() function is used to determine the type of a variable. It returns a string that indicates the type of the variable. This function is similar to the typeof() function in PHP, but it is a more commonly used function because it is more intuitive and easier to understand.

The syntax for the gettype() function is as follows:

string gettype(mixed $var)

The gettype() function takes a single argument, $var, which can be any variable or expression. The function returns a string that indicates the data type of the variable.

The gettype() function returns one of the following strings:

  • “boolean” – if the variable is a boolean
  • “integer” – if the variable is an integer
  • “double” – if the variable is a floating-point number
  • “string” – if the variable is a string
  • “array” – if the variable is an array
  • “object” – if the variable is an object
  • “resource” – if the variable is a resource
  • “NULL” – if the variable is null
  • “unknown type” – if the variable is of an unknown type

Let’s look at some examples of how the gettype() function can be used.

Example 1: Using gettype() with a string variable

$var = "Hello World!";
echo gettype($var); // outputs "string"

In this example, the gettype() function is used to determine the data type of the variable $var, which is a string. The function returns the string “string”.

Example 2: Using gettype() with an integer variable

$var = 10;
echo gettype($var); // outputs "integer"

In this example, the gettype() function is used to determine the data type of the variable $var, which is an integer. The function returns the string “integer”.

Example 3: Using gettype() with an array variable

$var = array(1, 2, 3);
echo gettype($var); // outputs "array"

In this example, the gettype() function is used to determine the data type of the variable $var, which is an array. The function returns the string “array”.

Example 4: Using gettype() with a null variable

$var = null;
echo gettype($var); // outputs "NULL"

In this example, the gettype() function is used to determine the data type of the variable $var, which is null. The function returns the string “NULL”.

In conclusion, The gettype() function is a commonly used tool for determining the data type of a variable in PHP. It is more intuitive and easier to understand than the typeof() function, and it can be used in many different situations to help you debug your code or perform different actions based on the data type of a variable. By understanding how the gettype() function works, you can become a more efficient and effective PHP developer.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *