Warning: Invalid argument supplied for foreach() in views_php_handler_filter->php_post_execute()
The error is here:

$view-&gt;result = null;


Try changing it to:

$view-&gt;result = array();


Views is running over the $view-&gt;result var as an array without checking its type, but it's always expecting an array. As NULL isn't an array it complains, but it's just a Warning so the script continues. Because the results loop has failed you get the same effect as when the array is empty, and as you say everything works as expected.