Menu
Coddy logo textTech

Recap: Function Medley

Part of the Logic & Flow section of Coddy's PHP journey — lesson 9 of 68.

challenge icon

Challenge

Easy

Create 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