Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<BackedEnum::fromSensitiveParameterValue>
^BackedEnumLast updated: Mon, 26 Oct 2026

BackedEnum::tryFrom

(PHP 8 >= 8.1.0)

BackedEnum::tryFrom — Maps a scalar to an enum instance or null

Description

public static function BackedEnum::tryFrom(int|string $value): ?static

The tryFrom() method translates a string or int into the corresponding Enum case, if any. If there is no matching case defined, it will return null.

Parameters

value

The scalar value to map to an enum case.

Return Values

A case instance of this enumeration, or null if not found.

Examples

Example #1 Basic usage

The following example illustrates how enum cases are returned.

<?php
enum Suit: string
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

$h = Suit::tryFrom('H');

var_dump($h);

$b = Suit::tryFrom('B') ?? Suit::Spades;

var_dump($b);
?>

The above example will output:

enum(Suit::Hearts)
enum(Suit::Spades)

See Also


<BackedEnum::fromSensitiveParameterValue>
^BackedEnumLast updated: Mon, 26 Oct 2026