PHP TypeLang Help

String Coercion

Cast passed value into a PHP string type.

The StringTypeCoercer takes one optional floatPrecision parameter.

This parameter responsible for maximum precision when converting from float to string. If the value is not specified (null), then the precision corresponds to the value of precision in php.ini.

$coercer = new TypeLang\Mapper\Type\Coercer\StringTypeCoercer( // Expects: int<1, 53>|null floatPrecision: 42, );

Input Value

Output Result

Description

string(T)

string(T)

Returns the string value "as is"

null

string("")

bool(true)

string("true")

bool(false)

string("false")

float(INF)

string("inf")

float(-INF)

string("-inf")

float(NAN)

string("nan")

float(T)

string(T)

Converts float to a string value [1]

int(T)

string(T)

Converts int to a string value [2]

Stringable

string(T)

Converts object to a string value [3]

resource(T)

string(T)

Converts resource to a string value [4]

BackedEnum

string(BackedEnum::$value)

Returns the value of the backed enum's case

UnitEnum

string(UnitEnum::$name)

Returns the name of the unit enum's case

Other

never

Throws InvalidValueException exception

Notes

  1. The float-to-string casting uses built-in PHP rules:

    • The float value accuracy depends on the precision value in php.ini file.

    • If a loss of precision occurs during conversion to a string, an InvalidValueException error will occur.

  2. The int-to-string casting uses built-in PHP rules.

  3. For objects implementing the Stringable interface, the __toString() method will be called.

  4. Returns the resource name, such as "stream" for file streams. If the resource is unknown, then simply the string "resource" will be returned.

05 November 2025