logo CodeStepByStep logo


Language/Type: PHP arrays
Author: Melissa Hovik (on 2018/01/05)

Write a function square_nums that takes an array of integers as an argument and returns a new array with the squares of all of the values of the passed array.

For example, if an array named $arr stores the values [0, 1, 2, 3, 4, 5], the call square_nums($arr) should return a new array containing the values [0, 1, 4, 9, 16, 25]. If $arr had instead contained [-1, 4, -5, 25], the call square_nums($arr) would be expected to return a new array with the values [1, 16, 25, 625].


  • Do not modify the passed array.
Function: Write a PHP function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.