Question: Here php function

(Update after reading comments etc.)

Yes, `$db` is defined outside and above function

```
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass//, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

function vatRates () {
   $something = "test";
   echo $something;

try {
   $query_select_date = "SELECT CurrencyRate FROM 2013Currencies WHERE DateOfCurrencyRate = '2001-03-23'";

   $sql_select_date = $db->prepare($query_select_date);
   $sql_select_date->execute();
   $data_select_date = $sql_select_date->fetchAll(PDO::FETCH_ASSOC);
   }
 catch (PDOException $e){
 echo "<br>DataBase Error: " .$e->getMessage();
 }
 catch (Exception $e) {
 echo "General Error: ".$e->getMessage() .'<br>';
 }

   foreach ($data_select_date as $data) {
       echo($data[CurrencyRate]);
   }

}

```

And then call the function with `vatRates();`

In output get only word: test from `$something`. And php code that is after `vatRates();` does not execute.

But if delete `function vatRates () {` and closing `}` then mysql query and foreach works.

Why mysql query and foreach does not work inside function (what need to correct)?

Answer: 
Function must be return something, a string, a array, a bolean, etc..

Better is (with arg $db passed):

```
function vatRates ($db) {
$CurrencyRate = "";

$query_select_date = "SELECT CurrencyRate FROM 2013Currencies WHERE DateOfCurrencyRate = '2001-03-23'";

$sql_select_date = $db->prepare($query_select_date);
$sql_select_date->execute();
$data_select_date = $sql_select_date->fetchAll(PDO::FETCH_ASSOC);

foreach ($data_select_date as $data) {

$CurrencyRate .= $data[CurrencyRate];
}
return $CurrencyRate;
}

```

To use it:

```
echo vatRates($db);

```
```
function vatRates () {
   $something = "test";
   echo $something;
   global $db;

   /* Your sql code and foreach goes here*/

}

```

**or**

```
function vatRates ($db) {
       $something = "test";
       echo $something;

       /* Your sql code and foreach goes here*/

 }

```