I have used PHP for a very long time, but not really used callbacks very much until quite recently. In the following code, the callback (the example is QueryPath, in case you're wondering, but it could be anything that accepts a callback) will add some link to an array:
// parse any product links out of the html
$aProducts = array();
qp( $html, 'a' )->each(function($index, $element){
global $aProducts;
$link = qp($element)->attr('href');
$pregMatch = preg_match('@(.*)-p-(.*)\.html@i', $link, $matches);
if( $pregMatch ) {
$product_id = (int)$matches[2];
if( !in_array($product_id, $aProducts) ) {
$aProducts[] = $product_id;
}
}
});
// print out our product array
print_r( $aProducts );
What's the alternative to using global $aProducts
(if there is one)?