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 trialJojo Ind
2,260 PointsHow can this line "if(is_array($arr)){" check if there's an array ?
How can this line "if(is_array($arr)){" check if there's an array ? Since the variable name of the array is different - which is "$names"?
Thank you Jo
3 Answers
Chris Shaw
26,676 PointsHi Jojo,
In PHP and many other languages we have the ability to use a feature called function arguments which is what Hampton is using in the video, a function argument in context of the video is $arr
. The variable $arr
is using a feature known in PHP called Pass-by-Reference which takes a copy of the value we pass to the function and allows us to reuse it but with a different name inside our function.
One key thing to remember is our function arguments are scoped locally only to our function, this means that if we have a variable outside our function with the same name as the on inside it, the one on the inside will supersede it because it's within a different scope.
You can learn more about function arguments at the below link.
http://php.net/manual/en/functions.arguments.php
Happy coding!
Christopher Wehkamp
1,926 PointsI feel like this only answered 1/2 the question. Chris Upjohn's answer addresses Jo's question about why this can still work even when the variable names $arr and $names are different.
But I don't see any answer describing how the is_array($arr) internal function works. Unless I am missing something, nothing in the PHP track I am on has addressed what an "internal function" means or how it is working in this example.
Thanks!
Chris Shaw
26,676 PointsHi Christopher,
The internal workings are a bit confusing as they use native C++ functionality to determine the type of the variable, essentially what occurs is that the variable you pass is sent to a Zend function definition called Z_TYPE
which is an alias of zval_get_type
.
Z_TYPE
returns a type reference that contains information about our variable. However, the value returned can't be used to determine the type as the standard PHP data types are defined as integers. Instead, another function definition called Z_TYPE_P
is used in conjunction with another internal function called php_is_type
.
Z_TYPE_P
, unlike Z_TYPE
returns an integer from the pointer reference that gets returned which corresponds to the standard data type integers, once all the data needed to determine a match is acquired, php_is_type
is used to return either TRUE
or FALSE
.
As an overview, see the below links for all the code that makes is_array
work like magic.
- Standard PHP data types
Z_TYPE
andZ_TYPE_P
definitionsphp_is_type
internal functionis_array
function reference
Hope that helps answer your question.
Christopher Wehkamp
1,926 PointsVery thorough answer Chris Upjohn, thanks!
Liam Brennan
17,182 PointsLiam Brennan
17,182 PointsJust the answer I was looking for, thank you!