PHP 8.3.4 Released!

gettype

(PHP 4, PHP 5, PHP 7, PHP 8)

gettypeObtém o tipo de uma variável

Descrição

gettype(mixed $value): string

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

Parâmetros

value

A variável a ter o tipo verificado.

Valor Retornado

Os valores possíveis para a string retornada são:

  • "boolean" - booleano
  • "integer" - inteiro
  • "double" - ponto flutuante (por razões históricas, "double" é retornado no caso de um float, e não simplesmente "float")
  • "string" - cadeia de caracteres
  • "array"
  • "object" - objeto
  • "resource" - recurso
  • "resource (closed)" a partir do PHP 7.2.0 - recurso (fechado)
  • "NULL" - nulo
  • "unknown type" - tipo desconhecido

Registro de Alterações

Versão Descrição
7.2.0 Recursos já fechados agora são reportados como 'resource (closed)'. Anteriormente os valores retornados para recursos fechados eram 'unknown type'.

Exemplos

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

<?php

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

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

?>

O exemplo acima produzirá algo semelhante a:

integer
double
NULL
object
string

Veja Também

  • get_debug_type() - Obtém o nome do tipo de uma variável de forma adequada a depuração
  • settype() - Define o tipo de uma variável
  • get_class() - Retorna o nome da classe de um objeto
  • is_array() - Verifica se a variável é um array
  • is_bool() - Verifica se a variável é um booleano
  • is_callable() - Verifica se um valor pode ser chamado como uma função a partir do escopo atual.
  • is_float() - Verifica se a variável é do tipo float
  • is_int() - Informa se o tipo de uma variável é um inteiro
  • is_null() - Verifica se uma variável é null
  • is_numeric() - Verifica se uma variável é um número ou uma string numérica
  • is_object() - Verifica se uma variável é um objeto
  • is_resource() - Verifica se uma variável é um recurso
  • is_scalar() - Verifica se é uma váriavel é escalar
  • is_string() - Verifica se o tipo de uma variável é string
  • function_exists() - Retorna true se a função informada estiver definida
  • method_exists() - Verifica se o método da classe existe

add a note

User Contributed Notes 2 notes

up
8
mohammad dot alavi1990 at gmail dot com
9 months ago
Be careful comparing ReflectionParameter::getType() and gettype() as they will not return the same results for a given type.

string - string // OK
int - integer // Type mismatch
bool - boolean // Type mismatch
array - array // OK
up
-49
Anonymous
2 years ago
Same as for "boolean" below, happens with integers. gettype() return "integer" yet proper type hint is "int".

If your project is PHP8+ then you should consider using get_debug_type() instead which seems to return proper types that match used for type hints.
To Top