Is it possible to use a PHP constant within a PHP function?
// in a different file
DEFINE ('HOST', 'hostname');
DEFINE ('USER', 'username');
DEFINE ('PASSWORD', 'password');
DEFINE ('NAME', 'dbname');
// connecting to database
function database()
{
// using 'global' to define what variables to allow
global $connection, HOST, USER, PASSWORD, NAME;
$connection = new mysqli(HOST, USER, PASSWORD, NAME)
or die ('Sorry, Cannot Connect');
return $connection;
}
You don’t need to declare them in
globalin the function, PHP recognises them as globals.From php.net: