gettype


php128 apg

GET the type of a VARIABLE.

This function returns a STRING, as type name, of a variable when it tests the following types:

STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.

Although it looks practical, it is not recommended in situations involving redefinitions of variable values, with possible type changes.

The type of the same variable at a given time may not be the same as defined above.

Instantly, using one of the is_* functions can be more reliable.



<?php

str gettype 
$arg );


where,

$arg ARGUMENT to test
      
?>

$arg


The argument to test.





To make it easier to recognize the TYPE of a variable and save service, we will use an user-defined function which can be seen below:



<?php

   
function get_type($arg)
            {
            
$type gettype($arg);
            echo 
$type '<br>';
            
var_dump($arg);
            echo 
'<br><br>';
            }

where,

$arg ARGUMENT to test

?>

  1 EXERCISE   

<?php

function get_type($arg

$type gettype($arg); 
echo 
$type '<br>'
var_dump($arg); 
echo 
'<br><br>'



$v01a NULL;
$v01b 100;
$v01c = array(2'alô''hello');
$v01d true;
$v01e 'Quandoque bonus dormitat Homerus!';
$v01f M_2_PI;

/* - - - - - - - - - - - - - - - - - - - - - - - -
In this exercise, we will only show examples of
RESOURCE and OBJECT types.

You should not bother to understand 
them at the moment.

We will be back to treat them in the near future 
- - - - - - - - - - - - - - - - - - - - - - - - - */

$v01g imagecreate(1,2);
// RESOURCE

class nwteste
{
var 
$var1 'Isto é um objeto teste';
var 
$var2 'This is a test object';
}
$v01h = new nwteste;
// echo $v01h->var1;
// echo $v01h->var2;
// OBJECT

get_type($v01a);
get_type($v01b);
get_type($v01c);
get_type($v01d);
get_type($v01e);
get_type($v01f);
get_type($v01g);

get_type($v01h);
echo 
'<br>';
echo 
$v01h->var1 '<br>';
echo 
$v01h->var2 '<br><br>';

?>


 RESULT   

NULL
NULL

integer
int(100)

array
array(3) { [0]=> int(2) [1]=> string(4) "alô" [2]=> string(5) "hello" }

boolean
bool(true)

string
string(33) "Quandoque bonus dormitat Homerus!"

double
float(0.63661977236758)

- - - - - - - - - - - - - - - - - - - - - - - - - -
PHP 7.4.XX
resource
resource(11) of type (gd)

or

PHP 8.0.XX
object
object(GdImage)#1 (0) { }
- - - - - - - - - - - - - - - - - - - - - - - - - -

object
object(nwteste)#1 (2) { ["var1"]=> string(23) "Isto é um objeto teste" ["var2"]=> string(21) "This is a test object" }

Isto é um objeto teste
This is a test object