Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Introducing Functions PHP Function Arguments

Ker Sing Tan
Ker Sing Tan
10,573 Points

is_array($arr) question

is_array($arr) is placing before foreach loop, means that we will validate if $arr is an array before we run foreach loop. So in this case, why should we set the array to $arr = array (name1, name2) instead? In the example, we assign array to '$name' and $name come after the is_array($arr) validation, so I suppose the is_array($arr) should equal to false?

1 Answer

nathanl
nathanl
9,458 Points

Functions are not executed automatically when the page loads; they are only executed when called.

In the example, you have a function named hello which validates whether or not the argument is an array. The actions in this function are only performed when hello($names) is executed at the end.

For clarity the order of execution is as follows:

  1. The $names variable is set to the array
  2. The hello() function is called with the $names variable passed in as the argument
  3. The hello() function is run and performs the defined actions