gettype

(PHP 4, PHP 5, PHP 7)

gettypeObtém o tipo da variável

Descrição

gettype ( mixed $var ) : string

Retorna o tipo da variável no PHP var. Para checagem de tipo, utilize as funções is_*.

Parâmetros

var

A variável a ter o tipo verificado.

Valor Retornado

Os possíveis valores retornados pela função são:

  • "boolean"
  • "integer"
  • "double" (por razões históricas "double" é é retornado no caso de float, e não simplesmente "float")
  • "string"
  • "array"
  • "object"
  • "resource"
  • "NULL"
  • "unknown type"

Exemplos

Exemplo #1 Exemplo da função gettype()

<?php

$data 
= array(11.NULL, new stdClass'foo');

foreach (
$data as $value) {
    echo 
gettype($value), "\n";
}

?>

O exemplo acima irá imprimir algo similar à:

integer
double
NULL
object
string

Veja Também

add a note add a note

User Contributed Notes 1 note

up
-15
matt at appstate
18 years ago
Here is something that had me stumped with regards to gettype and is_object.
Gettype will report an incomplete object as such, whereas is_object will return FALSE.

<?php
if (!is_object($incomplete_obj)) {
   echo
'This variable is not an object, it is a/an ' . gettype($incomplete_obj);
}
?>

Will print:
This variable is not an object, it is a/an object
To Top