Hướng dẫn dùng typeof syntax trong PHP

❮ PHP Variable Handling Reference

Nội dung chính

  • Definition and Usage
  • Parameter Values
  • Technical Details
  • Description
  • Return Values
  • What is the use of Settype () and Gettype () function in PHP?
  • What is get type function do?
  • What is $_ GET variable?
  • How do you convert one variable type to another in PHP?

Nội dung chính

  • Definition and Usage
  • Parameter Values
  • Technical Details
  • Description
  • Return Values
  • What is the use of Settype () and Gettype () function in PHP?
  • What is get type function do?
  • What is $_ GET variable?
  • How do you convert one variable type to another in PHP?

Example

Return the type of different variables:

<?php
$a = 3;
echo gettype($a) . "<br>";

$b = 3.2;
echo gettype($b) . "<br>";

$c = "Hello";
echo gettype($c) . "<br>";

$d = array();
echo gettype($d) . "<br>";

$e = array("red", "green", "blue");
echo gettype($e) . "<br>";

$f = NULL;
echo gettype($f) . "<br>";

$g = false;
echo gettype($g) . "<br>";
?>

Try it Yourself »


Definition and Usage

The gettype() function returns the type of a variable.


Syntax

Parameter Values

ParameterDescription
variable Required. Specifies the variable to check

Technical Details

Return Value:The type as a string. Can be one of the following values: "boolean", "integer", "double", "string", "array", "object", "resource", "NULL", "unknown type"
Return Type:String
PHP Version:4.0+
PHP Changelog:PHP 7.2: Closed resources are now returned as "resource (closed)". Earlier, the returned value was "unknown type".

❮ PHP Variable Handling Reference


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

gettypeGet the type of a variable

Description

gettype(mixed $value): string

Parameters

value

The variable being type checked.

Return Values

Possible values for the returned string are:

  • "boolean"
  • "integer"
  • "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" as of PHP 7.2.0
  • "NULL"
  • "unknown type"

Changelog

VersionDescription
7.2.0 Closed resources are now reported as 'resource (closed)'. Previously the returned value for closed resources were 'unknown type'.

Examples

Example #1 gettype() example

<?php

$data

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

foreach (

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

The above example will output something similar to:

integer
double
NULL
object
string

See Also

  • get_debug_type() - Gets the type name of a variable in a way that is suitable for debugging
  • settype() - Set the type of a variable
  • get_class() - Returns the name of the class of an object
  • is_array() - Finds whether a variable is an array
  • is_bool() - Finds out whether a variable is a boolean
  • is_callable() - Verify that a value can be called as a function from the current scope.
  • is_float() - Finds whether the type of a variable is float
  • is_int() - Find whether the type of a variable is integer
  • is_null() - Finds whether a variable is null
  • is_numeric() - Finds whether a variable is a number or a numeric string
  • is_object() - Finds whether a variable is an object
  • is_resource() - Finds whether a variable is a resource
  • is_scalar() - Finds whether a variable is a scalar
  • is_string() - Find whether the type of a variable is string
  • function_exists() - Return true if the given function has been defined
  • method_exists() - Checks if the class method exists

Anonymous

7 months 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.

What is the use of Settype () and Gettype () function in PHP?

The gettype() function gets the type of variable; gettype() is a function that display a data type. The settype function sets the type of variable; the settype() function changes the data type.

What is get type function do?

The gettype() function returns the type of a variable.

What is $_ GET variable?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>

How do you convert one variable type to another in PHP?

PHP settype() Function The settype() function converts a variable to a specific type.