downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

import_request_variables> <get_resource_type
Last updated: Fri, 30 Oct 2009

view this page in

gettype

(PHP 4, PHP 5)

gettypeLiefert den Datentyp einer Variablen

Beschreibung

string gettype ( mixed $var )

Liefert den Datentyp einer Variablen. var .

Warnung

Benutzen Sie gettype() niemals, um auf einen bestimmten Typ zu prüfen, da die zurückgegebene Zeichenkette irgendwann geändert werden könnte. Außerdem ist diese Methode langsam, da sie einen Vergleich von Zeichenketten benutzt.

Verwenden Sie stattdessen die is_*-Funktionen.

Parameter-Liste

var

Die Variable, deren Typ ermittelt werden soll.

Rückgabewerte

Mögliche Werte der zurückgegebenen Zeichenkette sind:

Beispiele

Beispiel #1 gettype()-Beispiel

<?php

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

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

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

integer
double
NULL
object
string

Siehe auch

  • settype() - Legt den Typ einer Variablen fest
  • is_array() - Prüft, ob die Variable ein Array ist
  • is_bool() - Prüft, ob eine Variable vom Typ boolean ist
  • is_float() - Prüft, ob eine Variable vom Typ float ist
  • is_int() - Prüft, ob eine Variable vom Typ int ist
  • is_null() - Prüft ob eine Variable NULL enthält
  • is_numeric() - Prüft, ob eine Variable eine Zahl oder ein numerischer String ist
  • is_object() - Prüft, ob eine Variable vom Typ object ist
  • is_resource() - Prüft, ob eine Variable vom Typ resource ist
  • is_scalar() - Prüft ob eine Variable skalar ist
  • is_string() - Prüft, ob Variable vom Typ string ist
  • function_exists() - Falls die angegebene Funktion definiert ist, wird TRUE zurück gegeben
  • method_exists() - Prüft on eine Methode innerhalb eines Objekts existiert



import_request_variables> <get_resource_type
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
gettype
Andaramuxo
01-Aug-2009 11:33
Error:
<?php
$x
= pow(16,1/2);
echo
gettype($x);
?>
This will return "double" but $x after convert is 4.
skatebiker at hotmail dot com
22-Feb-2008 02:51
In some rare cases a class instance object returns false when an object but gettype() returns "object".

$x = new classvar();

$save = serialize($x);

......

$obj = unserialize($save);
// here sometimes is_object() returns FALSE
if (is_object($x) || gettype($x) === "object")
{
   ... do something
}
andrey at php dot net
17-Jul-2007 07:08
The function returns "unicode" for Unicode strings in PHP6.
sneskid at hotmail dot com
05-Mar-2007 10:56
I wrote my own gettype function by just using the default is_? functions, but it took twice as long as gettype... So I decided to use gettype with a twist.

Taking the warnings about gettype to heart, and depending on your custom needs, it's worthwhile to dynamically test the gettype result with a known variable, and link the result to a predefined result. Like so:

<?php
/*
 dynamically create an array by using known variable types
 link with a predefined value
*/
$R=array();
$R[gettype(.0)]='number';
$R[gettype(0)]='number';
$R[gettype(true)]='boolean';
$R[gettype('')]='string';
$R[gettype(null)]='null';
$R[gettype(array())]='array';
$R[gettype(new stdClass())]='object';

// what is
function wis_($v){
    global
$R;
    return
$R[gettype($v)];
}

echo
wis_('hello') . '<br/>'; // "string"
echo wis_(24) . '<br/>'; // "number"
echo wis_(0.24) . '<br/>'; // "number"
echo wis_(null) . '<br/>'; // "null"
echo wis_($R) . '<br/>'; // "array"
?>
You won't need to worry about changes in gettype's return strings in future versions.
If the result evaluates to false then you know the variable tested is some "other" type.

I also find these useful
<?php
function is_num($v){return (is_int($v) || is_double($v));}
function
is_box($v){return (is_array($v)||is_object($v));}

echo
is_num(null) . '<br/>'; // false
echo is_num(false) . '<br/>'; // false
echo is_num('123') . '<br/>'; // false
echo is_num(123) . '<br/>'; // true
echo is_num(123.0) . '<br/>'; // true
?>
gilthansNOSPAM at gmail dot com
11-Sep-2005 10:18
NaN and #IND will return double or float on gettype, while some inexistent values, like division by zero, will return as a boolean FALSE. 0 by the 0th potency returns 1, even though it is mathematically indetermined.

<?php
$number
= 5/0;
$number2 = sqrt(-3);
$number3 = pow(0, 0);
$number4 = 0/0;

echo
$number."<br />";
echo
$number2."<br />";
echo
$number3."<br />";
echo
$number4."<br />";
echo
"<br />";
echo
gettype($number)."<br />";
echo
gettype($number2)."<br />";
echo
gettype($number3)."<br />";
echo
gettype($number4);
?>

This will return:

-1.#IND
1

boolean
double
integer
boolean

0
1
1
0
PHP Warning: Division by zero in C\test.php on line 2 PHP Warning: Division by zero in C:\test.php on line 5
matt at appstate
16-Dec-2004 08:10
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.

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

import_request_variables> <get_resource_type
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites