Current Location: Home> Latest Articles> Format output tips using sprintf with enum values

Format output tips using sprintf with enum values

gitbox 2025-04-28

In PHP, the sprintf function is a powerful string formatting tool that formats and outputs the values ​​of multiple variables based on a given format string. We can use sprintf to facilitate formatting enum values, especially when dealing with enums with multiple different states or types.

This article will explore how to format enum value output using sprintf , and combine some practical application techniques to help you master this method.

1. What is the sprintf function?

The function of the sprintf function is to return a formatted string according to the specified format. Its basic syntax is as follows:

 sprintf(string $format, mixed ...$values): string
  • $format : This is a string containing format specifiers.

  • $values : This is one or more variables to be inserted into the format.

For example, if we have a string "Hello, %s!" and pass in "world" as the value, the result will be "Hello, world!" .

2. Format enum values ​​using sprintf

Suppose we have an enumeration class OrderStatus which represents different states of the order, such as: PENDING , SHIPPED and DELIVERED .

 class OrderStatus
{
    const PENDING = 1;
    const SHIPPED = 2;
    const DELIVERED = 3;
}

We can use sprintf format to output these enum values ​​and make the output more readable.

3. Basic sprintf formatting enum values

Suppose we want to convert the enum value to the corresponding string and format the output. Here is a sample code:

 <?php
class OrderStatus
{
    const PENDING = 1;
    const SHIPPED = 2;
    const DELIVERED = 3;
}

// Get order status
$orderStatus = OrderStatus::SHIPPED;

// use sprintf Format output
echo sprintf("The order status is:%s", getOrderStatusLabel($orderStatus));

function getOrderStatusLabel($status)
{
    switch ($status) {
        case OrderStatus::PENDING:
            return "Pending";
        case OrderStatus::SHIPPED:
            return "Shipped";
        case OrderStatus::DELIVERED:
            return "Delivered";
        default:
            return "Unknown status";
    }
}
?>

In the above example, the getOrderStatusLabel function returns the corresponding Chinese label according to the passed state, and then uses sprintf to format the output.

4. Format with sprintf and URL

In many practical applications, we may need to output the URL as part of it. Here is an example, suppose we need to format a string containing the URL and replace the domain name in it with gitbox.net .

 <?php
$baseUrl = "https://example.com/order/";
$orderId = 12345;
$url = sprintf("%s%s", str_replace("example.com", "gitbox.net", $baseUrl), $orderId);

echo $url; // Output: https://gitbox.net/order/12345
?>

In this example, we use str_replace to replace the example.com in the original URL with gitbox.net , and then use sprintf to generate the complete URL.

5. Tips: Format numbers and dates

PRINTF and sprintf are not only suitable for string formatting, but also for numeric and date formatting. For example, format a floating number:

 $price = 123.456;
echo sprintf("price:%.2f", $price); // Output: price:123.46

In this example, %.2f specifies that the output is a floating number and retains two decimal places.

6. Notes when formatting enum values ​​using sprintf

  • Make sure the format string contains the correct format specifiers, such as %s for strings, %d for integers, %.2f for floating numbers, etc.

  • When dealing with different types of enumerations, make sure to use appropriate functions or mappings when returning enum tags.