Recap: Function Medley
Part of the Logic & Flow section of Coddy's PHP journey — lesson 9 of 68.
Challenge
EasyCreate a text processing system that combines multiple function concepts you've learned. You'll need to implement several functions and use them together to process text data.
First, create an anonymous function stored in a variable called $reverseText that takes a string and returns it reversed.
Next, create a regular function called countVowels that takes a string and returns the number of vowels (a, e, i, o, u - case insensitive) in it.
Then, create a function called processText that accepts three parameters: a text string, an operation name, and a callback function. This function should:
- If the operation is
"reverse", apply the callback to reverse the text and return the result - If the operation is
"vowels", apply the callback to count vowels and return the count - If the operation is
"length", return the length of the text
You will receive two inputs: the operation name ("reverse", "vowels", or "length") and the text to process. Read both inputs, determine which function to use based on the operation, call processText with the appropriate parameters, and print the result.
Input format: Two lines - first line contains the operation name, second line contains the text to process
Expected output: The result based on the operation - reversed text for "reverse", vowel count for "vowels", or text length for "length"
Try it yourself
<?php
// Read input
$operation = trim(fgets(STDIN));
$text = trim(fgets(STDIN));
// TODO: Write your code below
// Create the $reverseText anonymous function
// Create the countVowels function
// Create the processText function
// Call processText with appropriate parameters based on the operation
// Output the result
echo $result;
?>All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting