Title: rest_get_best_type_for_value
Published: August 11, 2020
Last modified: August 20, 2026

---

# rest_get_best_type_for_value( mixed $value, string[] $types ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#wp--skip-link--target)

Gets the best type for a value.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#parameters)󠁿

 `$value`mixedrequired

The value to check.

`$types`string[]required

The list of possible types.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#return)󠁿

 string The best matching type, an empty string if no types match.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#source)󠁿

    ```php
    function rest_get_best_type_for_value( $value, $types ) {
    	static $checks = array(
    		'array'   => 'rest_is_array',
    		'object'  => 'rest_is_object',
    		'integer' => 'rest_is_integer',
    		'number'  => 'is_numeric',
    		'boolean' => 'rest_is_boolean',
    		'string'  => 'is_string',
    		'null'    => 'is_null',
    	);

    	/*
    	 * Both arrays and objects allow empty strings to be converted to their types.
    	 * But the best answer for this type is a string.
    	 */
    	if ( '' === $value && in_array( 'string', $types, true ) ) {
    		return 'string';
    	}

    	foreach ( $types as $type ) {
    		if ( isset( $checks[ $type ] ) && $checks[ $type ]( $value ) ) {
    			return $type;
    		}
    	}

    	return '';
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api.php#L1721)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api.php#L1721-L1747)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#related)󠁿

| Used by | Description | 
| [rest_handle_multi_type_schema()](https://developer.wordpress.org/reference/functions/rest_handle_multi_type_schema/)`wp-includes/rest-api.php` |

Handles getting the best type for a multi-type schema.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.5.0](https://developer.wordpress.org/reference/since/5.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Frest_get_best_type_for_value%2F)
before being able to contribute a note or feedback.