Is there a way to use a foreach of a query within a function

1

I have a simple query :

$Consulta = $pdo->query(" SELECT * FROM dados ")->fetchAll();
foreach ($Consulta as $key) 
{
     echo $key["InfoDado"];
}

If you run foreach out of a function! beauty! It works as it should!

But I tried to execute this foreach inside a function and it gave error!

Can you execute this foreach within a function? Something like:

function ExecutarForeach()
{
    foreach ($Consulta as $key) {
        echo $key["InfoDado"];
    }
};

ExecutarForeach();
    
asked by anonymous 04.09.2016 / 22:22