PHP 8.3.4 Released!

gettype

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

gettypeObtener el tipo de una variable

Descripción

gettype(mixed $var): string

Devuelve el tipo de la variable PHP var. Para la comprobación de tipos, utilice las funciones is_*.

Parámetros

var

La variable de la cual queremos comprobar su tipo.

Valores devueltos

Los valores posibles para la cadena devuelta son:

Ejemplos

Ejemplo #1 Ejemplo de gettype()

<?php

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

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

?>

El resultado del ejemplo sería algo similar a:

integer
double
NULL
object
string

Ver también

  • settype() - Establece el tipo de una variable
  • get_class() - Devuelve el nombre de la clase de un objeto
  • is_array() - Comprueba si una variable es un array
  • is_bool() - Comprueba si una variable es de tipo booleano
  • is_callable() - Verificar que los contenidos de una variable puedan ser llamados como una función
  • is_float() - Comprueba si el tipo de una variable es float
  • is_int() - Comprueba si el tipo de una variable es integer
  • is_null() - Comprueba si una variable es null
  • is_numeric() - Comprueba si una variable es un número o un string numérico
  • is_object() - Comprueba si una variable es un objeto
  • is_resource() - Comprueba si una variable es un recurso
  • is_scalar() - Comprueba si una variable es escalar
  • is_string() - Comprueba si una variable es de tipo string
  • function_exists() - Devuelve true si la función dada ha sido definida
  • method_exists() - Comprueba si existe un método de una clase

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