DEFINE constant in database with strtoupper function?

While I’m learning php/mysql, I was trying to connect the database with constant variables and foreach loop. My question is, if I have given the define constant to make it uppercase, in case by mistake if I make it small or user edit the constant, the foreach loop will correct it through strtoupper function but in my case it is not working, where I am doing wrong?

$db['db_host'] = 'localhost';
$db['db_user'] = 'root';
$db['db_pass'] = '';
$db['db_name'] = 'cms';

foreach ($db as $key => $value) {
	define(strtoupper($key), $value);
}

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); // If the letter is smallcase, can foreach make it uppercase?
if($conn === true);
echo 'We are Connected';

You can use a third parameter when defining the constants to make them case insensitive. Example:

define('usER', 'Bob', true);

var_dump(user);    // Works, prints 'Bob'
var_dump(USER);    // Works, prints 'Bob'
var_dump(UsEr);    // Works, prints 'Bob'

Thanks this works but can you tell em why the strtoupper function is not making it upper automatically?

you’re using a different code. because it works for me.

$db['db_host'] = 'localhost';
$db['db_user'] = 'root';
$db['db_pass'] = '';
$db['db_name'] = 'cms';

foreach ($db as $key => $value) {
	define(strtoupper($key), $value, false);
}
echo DB_HOST; // localhost

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.