Reputation: 31
I'm writing a function and it uses the $PDO variable in another file. I included the file at the beginning, but it's saying undeclared variable. I'm assuming it's because it's out of scope.
require './db/db.php';
session_start();
function createUser($username) {
}
What can I do to be able to reference the variable $PDO which is my PDO instance to use the database in my functions?
Upvotes: 0
Views: 77
Reputation: 701
Pass $PDO as an argument;
function function_name($PDO)
{
// Your function code
}
Upvotes: 1
Reputation: 12341
Assuming you didn't declare the "PDO instance" inside any external function or class, you should just pass it to the function as a parameter. So (if you're talking about your createUser
function)
createUser($PDO, $username) { }
And you'd call it like this: createUser($PDO, 'Foo');
.
Upvotes: 1
Reputation: 782785
Either pass the variable to the function as an argument, or put
global $PDO;
in the function.
Upvotes: 0