Jump to content

Very, very simple code question


TechnoDiver

Recommended Posts

I'm messing around with unconventional coding practices, just to .... practice. The following code isn't being used for any amazing projects or anything, it's just me messing around with things getting practice. It's a simple code but it keeps telling me the constants are undefined.

//defining the variables through an array
$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "cms";


//fancy way of making them constants
foreach($db as $key => $value) {
    define(strtoupper($key), $value);
}

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

if($conn) {
    echo "We are connected";
}

Simple, simple, very simple but can someone tell me why it's not creating the constants? All of them in $conn are coming back as undefined.

Link to comment
Share on other sites

works correctly for me.

if your posted code didn't create the constants, either -

  1. that's not the actual code that was executed, e.g. that exact code didn't get saved to and executed on the web server.
  2. there's more code involved, such as function definition(s), so that the scope of the $db array and the foreach(){} loop are different so that the foreach code failed (there would be a bunch of php notice/warning errors.)
Link to comment
Share on other sites

Yea, it works for me too.

I use VScodium and I'm getting the red lines under all those constants saying they're undefined, but I just tried to run it anyways and I get the "We are connected" echo.

So I've learned that the IDE isn't always correct in it's error reporting. Is this a known issue? Something I can correct?

The file tab is red (error red) the folder that contains the file in explorer is error red. Why is this coming back as error but still seems to be functioning?

Edited by TechnoDiver
Link to comment
Share on other sites

15 minutes ago, TechnoDiver said:

So I've learned that the IDE isn't always correct in it's error reporting. Is this a known issue? Something I can correct?

IDE's don't run code, they just analyze it.  

In the code above, your IDE can't figure out what the name of the constant being created by the define call is because the name is a dynamic value returned by the strtoupper function. 

You might be able to configure VScodium to not treat this as an error.  I use PHPStorm and it highlights this as just a Warning not an Error.  To make the problem go away though you need to define your constants individually in a way the IDE can understand.

Link to comment
Share on other sites

Ok, thanks. I'm obviously not using this particular code for anything, just practicing so I'll move forward.

Out of curiosity, if I wanted to deactivate this type of 'error return' in my IDE (VSCodium) what would I even look for? Is there a specific term for this type of occurrence?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.