rest_get_best_type_for_value() WordPress Function

The rest_get_best_type_for_value() function is used to get the best data type for a given value. This is useful when you need to know the type of data that will be returned by a function.

rest_get_best_type_for_value( mixed $value, array $types ) #

Gets the best type for a value.


Parameters

$value

(mixed)(Required)The value to check.

$types

(array)(Required)The list of possible types.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/rest-api.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 '';
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More