What are the potential pitfalls of using optional characters in PHP regex patterns?

Using optional characters in PHP regex patterns can lead to unexpected results if not handled properly. One potential pitfall is that the optional character may not match any input, causing the regex pattern to fail when it shouldn't. To solve this issue, it's important to use the "?" quantifier judiciously and ensure that the optional character is properly accounted for in the regex pattern.

// Example of using optional characters in PHP regex pattern with proper handling
$input = "apple";
$pattern = "/app(le)?/"; // Optional character "le"
if (preg_match($pattern, $input, $matches)) {
    echo "Match found: " . $matches[0];
} else {
    echo "No match found";
}