What are the best practices for comparing and displaying substrings in PHP to achieve a specific formatting goal?

When comparing and displaying substrings in PHP to achieve a specific formatting goal, it's important to use functions like substr() to extract the desired portion of a string and then compare or format it accordingly. Additionally, using functions like strcasecmp() or strcmp() can help with string comparisons. Finally, utilizing formatting functions like sprintf() can help achieve the desired output format.

$string = "Hello, World!";
$substring = substr($string, 0, 5); // Extract first 5 characters

// Compare substring using strcmp
if (strcmp($substring, "Hello") === 0) {
    echo "Substring matches 'Hello'";
}

// Format substring using sprintf
$formattedSubstring = sprintf("[%s]", $substring);
echo $formattedSubstring;