PHP Velho Oeste 2024

gettype

(PHP 4, PHP 5, PHP 7)

gettypeObține tipul unei variabile

Descrierea

gettype ( mixed $var ) : string

Întoarce tipul variabilei PHP var. Pentru verificarea tipului, utilizați funcțiile is_*.

Parametri

var

Variabila, tipul căreia se verifică.

Valorile întoarse

Valorile posibile pentru string-ul întors sunt:

  • "boolean"
  • "integer"
  • "double" (din motive istorice se întoarce "double" în cazul unui float, și nu pur și simplu "float")
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" începând cu PHP 7.2.0
  • "NULL"
  • "unknown type"

Exemple

Example #1 Exemplu gettype()

<?php

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

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

?>

Exemplul de mai sus va afișa ceva similar cu:

integer
double
NULL
object
string

Istoricul schimbărilor

Versiune Descriere
7.2.0 Resursele închise sunt acum raportate ca 'resource (closed)'. Anterior valoarea întoarsă pentru resursele închise era 'unknown type'.

A se vedea și

  • settype() - Stabilește tipul unei variabile
  • get_class() - Returns the name of the class of an object
  • is_array() - Determină dacă o variabilă este un array
  • is_bool() - Determină dacă o variablă este un boolean
  • is_callable() - Verifică dacă conținutul unei variabile poate fi apelat în calitate de funcție
  • is_float() - Determină dacă tipul unei variabile este float
  • is_int() - Determină dacă tipul unei variabile este integer
  • is_null() - Determină dacă o variabilă este null
  • is_numeric() - Determină dacă o variabilă este un număr sau un string numeric
  • is_object() - Determină dacă o variabilă este un obiect
  • is_resource() - Determină dacă o variabilă este o resursă
  • is_scalar() - Determină dacă o variabilă este un scalar
  • is_string() - Determină dacă tipul variabilei este string
  • function_exists() - Return true if the given function has been defined
  • method_exists() - Checks if the class method exists

add a note add a note

User Contributed Notes 2 notes

up
5
mohammad dot alavi1990 at gmail dot com
10 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
-46
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