How can you optimize the process of validating if a string starts with a specific character in PHP for better performance?

When validating if a string starts with a specific character in PHP, you can optimize the process by using the substr() function to extract the first character of the string and comparing it directly to the target character. This method avoids unnecessary overhead from using regular expressions or other string manipulation functions.

// Optimized method to validate if a string starts with a specific character
function startsWith($string, $char) {
    return substr($string, 0, 1) === $char;
}

// Example usage
$string = "hello";
$char = "h";
if (startsWith($string, $char)) {
    echo "String starts with $char";
} else {
    echo "String does not start with $char";
}