0

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?

3 Answers 3

1

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');.

Sign up to request clarification or add additional context in comments.

Comments

1

Pass $PDO as an argument;

function function_name($PDO)
{
    // Your function code
}

Comments

0

Either pass the variable to the function as an argument, or put

global $PDO;

in the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.