PHP不能将数据库连接包含到函数中

I am struggling immensely to contain a db connection within its own function. To keep it simple everything is in one file. I think I need to pass $db through the connect function, but I don't know how to declare it as an empty object. I tried making db a class, then placing the contents of db_connect into a __construct and using $db = new db; but that didn't work either.

function db_connect(){
try {
    $db = new PDO('mysql:host=xxx.xxx.com;dbname=xxx;charset=utf8', 'xxx', 'xxx');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
}

function getTeamCount($leagueId){
    db_connect();
    $teamCount = $db->prepare('SELECT ...................');
    $teamCount->execute();
    $teamCountResult = $teamCount->fetchAll(PDO::FETCH_ASSOC);
    $teamsCounted = $teamCount->rowCount();
......
......
}
function db_connect(){
    try {
        $db = new PDO('mysql:host=xxx.xxx.com;dbname=xxx;charset=utf8', 'xxx', 'xxx');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    return $db; //here!
}

function getTeamCount($leagueId){
    $db = db_connect();
    ...
}