开发者

Elegant PHP variables

An empty $movies array given to the for loop will yield a warning. Checking for null avoids this. Not that i see this as ugly or messy code i'd just like to know if there are any more elegant ways of handling these instances.

function get_db_movies($db_handle) {
    $query = "SELECT title FROM movies";
    $result = $db_handle->query($query);
    $movies = null;
    while($row = mysql_fetch_array($result)){
        $movie =开发者_高级运维 new Movie($row['title'], $db_handle);
        $movies[] = $movie;
    }
    return $movies;
}

$movies = get_db_movies($db_handle);

foreach($movies as $movie) {
    $imdbCrawl = new imdbCrawler($movie);
    if($imdbCrawl->verifyMatch() && $imdbCrawl->isMovieFound()) {
        $imdbCrawl->getRating();
        $imdbCrawl->getPlot();
        $movie->syncDatabase();
    } else {
        echo "Movie crawl failed: " . $movie->getTitle();
    }
}


Initialize $movies to an empty array instead of null:

$movies = array();


You may initialize an array before use to avoid the warning:

$movies = array();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜