gettype

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

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(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

add a note

User Contributed Notes 1 note

up
-19
Anonymous
1 year 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