MockReal Logo
Home
BlogPricing
For Employers →
  1. Home
  2. Questions
  3. 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`?

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 Developer

To 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:

  1. Use explode(',', $input) to create an array.
  2. Utilize array_map('intval', $array) to convert each element to an integer.
  3. Finally, use array_sum($intArray) to get the total.

More Questions

  • Can the value of a constant change during the script’s execution?
  • Can you extend a `Final` defined class?
  • Do you use Composer? If yes, what benefits have you found in it?
  • Explain the difference between GET and POST HTTP methods.
  • How can we get the IP address of the client?
  • How can you enable error reporting in PHP?
  • How does one prevent the following Warning ‘Warning: Cannot modify header information – headers already sent’ and why does it occur in the first place?
  • How we can get the number of elements in an array?
  • How would you declare a function that receives one parameter name `hello`?
  • Suppose you receive a form submitted by a post to subscribe to a newsletter. This form has only one field, an input text field named `email`. How would you validate whether the field is empty?
MockReal Logo

Spoken interview practice for technical professionals

Product

  • Pricing
  • Questions
  • Positions
  • Resumes
  • For Employers →

Company

  • Careers
  • About Us
  • Contact
  • Blog

Legal

  • Privacy Policy
  • Terms of Service
  • Cookie Policy

© 2025 MockReal. Spoken interview practice. All rights reserved.