The value of the variable `input` is a string `1,2,3,4,5,6,7`. How would you get the sum of the integers contained inside `input`?
TechnicalSenior php developerPHP DeveloperBackend DeveloperTo get the sum of integers from the string `input`, I would first use the explode function to split the string by commas, then convert the resulting array to integers and sum them up.
Here's how I would do it:
- Use explode(',', $input) to create an array.
- Utilize array_map('intval', $array) to convert each element to an integer.
- Finally, use array_sum($intArray) to get the total.