MCQs > IT & Programming > PHP > Which code snippet would give two functions that would take a string and return, if it starts or ends with the specified character/string? For example: $str='|apples}'; echo startsWith($str,'|'); //Returns true echo endsWith($str,'}'); //Returns true

PHP MCQs

Which of the following code snippets would give two functions that would take a string and return, if it starts or ends with the specified character/string?

For example: $str='{apples}'; echo startsWith($str,'|'); //Returns true echo endsWith($str,'}'); //Returns true

Answer

Correct Answer:

function startsWith($haystack, $needle) { return !strncmp($haystack, $needle, strlen($needle)); } function endsWith($haystack, $needle) { $length = strlen($needle); if ($length == 0) { return true; } return (substr($haystack, -$length) === $needle); }


Explanation:

Note: This Question is unanswered, help us to find answer for this one

PHP Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it

search

PHP Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it