如何在我的函数中写常量?

How to write the constant in my function?

function insert_data($array){
    $dbh=new PDO('sqlite:C:\test.sqlite');
    $sql = "INSERT INTO quote (symbol,price) VALUES (?,?)";
    $q = $dbh->prepare($sql);
    $q->execute($array);
    $dbh=null;
}  

我想将 C:\test.sqlite 定义为常量。

define('db_name','C:\test.sqlite');

但是我不能在我的函数中使用常量 insert_data。

function insert_data($array){
    $dbh=new PDO("sqlite:db_name");
    $sql = "INSERT INTO quote (symbol,price) VALUES (?,?)";
    $q = $dbh->prepare($sql);
    $q->execute($array);
    $dbh=null;
} 

发生错误:

Fatal error: Call to a member function execute() on a non-object in....

我不想把函数写成这样的格式:

 function insert_data($array,$db){
    $dbh=new PDO("sqlite:{$db}");
    $sql = "INSERT INTO quote (symbol,price) VALUES (?,?)";
    $q = $dbh->prepare($sql);
    $q->execute($array);
    $dbh=null;
} 

如何解决?

定义变量 - $db = 'C:\test.sqlite';

并做到 global.

function insert_data($array){
    global $db;
    $dbh=new PDO("sqlite:{$db}");
    $sql = "INSERT INTO quote (symbol,price) VALUES (?,?)";
    $q = $dbh->prepare($sql);
    $q->execute($array);
    $dbh=null;
} 

Read it here