MK, GB, and everybody else,
Let me start by saying that it was not my intension to call the developers incompetent. I was saying that allowing this kind of thing to happen would tend to leave that impression with users.
Secondly, this kind of thing can happen with PHP because it is not a strongly-typed language. For non-programmers, this means that the compiler (the program that turns the source code into something a computer can actually execute) or, in this case, the interpreter (kind of a compiler-on-the-fly) does not enforce matching data types between a function (a sub-program) that calls another function and the function it calls, as between corresponding arguments and the result returned. For example, this could not happen in C or C++ because every function is required to be declared before it is called, and the declaration must specify the type of each argument and the type of data that is returned. That way, if a function returns the wrong data type, the compiler would catch it as a compilation error before anyone got a chance to run it.
In this case, since PHP is interpreted rather than compiled, the error would be caught at a later point, but probably before the point of executing the return statement; in this case, it wasn't caught until after the called function returned and the calling function tried to use the data that was returned.
So how would I re-write it? For starters, I would write as if I was using a more strongly typed language. I'm not suggesting we scrap PHP for another language; that would be much too major a change and would require many programmer-hours to accomplish. While PHP does not use function declarations, perhaps we could require developers to put comments in their function definitions letting other developers know what type of data to expect it to return, as well as what type of arguments it expects.
GB's suggested patch is a step in the right direction. At least it would generate a more user-friendly (i.e., understandable) msg. when this condition occurs. A better solution would be to re-write environment_check_php_extensions so that at each point where the statement "return $result" (instead of "return $results") occurs, it generates a more specific error msg. describing which test failed. In the version of Moodle I have (I didn't want to take the time to download the latest), I found two of these: the first, when no version data is found; the second, when no PHP extensions section is found.
I don't know; maybe what the developer meant to do was to append $result to the $results array, and then return that array, as with the rest of the function. That would be a simple enough re-write to do.