Using Callback Functions in PHP
A commonly used technique when programming is to use callbacks. A callback is a piece of code that is passed to a function that is later executed by that function. In this article I will show you how to define and call callbacks in PHP, as well as a practical example of how callbacks can be useful.
To demonstrate how callbacks can be beneficial, assume we have a PHP script that when called will download an RSS feed from 10 different web sites. If it takes 2 seconds to download from each site, it will take 20 seconds to download from all sites.
The code to achieve this may look something like the following code. I’ve added a called to sleep() to demonstrate how slow this script may be.
$site,
‘data’ => ‘downloaded data here’
);
}
return $ret;
}
// imaginary list of sites to download from
$sites = array(
‘http://www.example1.com’,
‘http://www.example2.com’
// more sites…
);
// start downloading and store the return data
$data = downloadFromSites($sites);
// output the return data
foreach ($data as $row) {
echo sprintf(“Finished downloading from %s\n”, $row[‘site’]);
}
?>
I read a lot of interesting articles here.
Probably you spend a lot of time writing,
i know how to save you a lot of work, there is an online tool
that creates unique, google friendly posts in seconds,
just type in google – laranitas free content source