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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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