Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gettype and unknown type in php

Tags:

php

I've just started to practice with PHP built-in gettype() and its return value. This function is capable to return testing result such as boolean, integer, unknown type, etc. But among those testing result, there's one caught my eyes: unknown type.

After reading gettype() and trying to find some reference here, i can not get any.

So, the question is what kind of type can be categorized as unknown type? Is it possible or am I just missing reading something?

like image 870
justjoe Avatar asked Mar 21 '10 21:03

justjoe


People also ask

What is Gettype PHP?

The gettype() function is an inbuilt function in PHP which is used to get the type of a variable. It is used to check the type of existing variable. Syntax: string gettype ( $var ) Parameter: This function accepts a single parameter $var. It is the name of variable which is needed to be checked for type of variable.

How to know PHP variable type?

To check data type of any variable, PHP provide a function gettype(). PHP gettype() function returns the data type of the variable value passed as argument and returns a string which is a clear data type of the passed value, like: integer, double, string etc.

What is the use of Settype () and Gettype () function in PHP?

The gettype() function gets the type of variable; gettype() is a function that display a data type. The settype function sets the type of variable; the settype() function changes the data type.

Which function returns the data type of a variable?

The gettype() function returns the type of a variable.


1 Answers

Here is one unknown type for you:

$f = fopen('somefile.txt','r');
echo gettype($f); // resource
fclose($f); 
echo gettype($f); // unknown

Basically, whenever a resource pointer is closed, the variable holding the handle will point to an unknown resource. Another example would be with GD'S imagecreate/imagedestroy.

Note: as of PHP 7.2, this no longer holds true. gettype will return resource (closed) then.

like image 116
Gordon Avatar answered Oct 05 '22 11:10

Gordon