Function - PHP Variable Scope issue

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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?

Answers

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/19106401/php-variable-scope-issue

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils