do you know how to remove vscode error underline in php when i define constant?

Viewed 79

in my vscode I define constant and when i want to print it, vscode gives error and red underline. but it works (prints). I want to remove this error underline but how, i don't know. can you help?

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

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

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

IT works. but VSCODE marks red underline at bottom DB_HOST,DB_USER, DB_PASS, DB_NAME.

I FIX this problem just change code like :

 $connection = mysqli_connect(constant("DB_HOST"), constant("DB_USER"), constant("DB_PASS"), constant("DB_NAME"));
1 Answers

VS Code doesn't actually execute your code, so it doesn't know the constants you have just defined in your code. It cannot pick up the defines executed within the loop.

If you replace your loop with

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'cms');

It should be smart enough to detect the define commands and the names that have been passed to it, and not give you any error.

Alternatively, do you need to define these variables? You could just use the $db variable to populate your connection:

$connection = mysqli_connect($db['db_host'], $db['db_user'], $db['db_pass'], $db['db_name']);
Related