TechnoDiver Posted June 14, 2021 Share Posted June 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312905-very-very-simple-code-question/ Share on other sites More sharing options...
mac_gyver Posted June 14, 2021 Share Posted June 14, 2021 works correctly for me. if your posted code didn't create the constants, either - 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. 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.) Quote Link to comment https://forums.phpfreaks.com/topic/312905-very-very-simple-code-question/#findComment-1587216 Share on other sites More sharing options...
TechnoDiver Posted June 14, 2021 Author Share Posted June 14, 2021 (edited) 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 June 14, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/312905-very-very-simple-code-question/#findComment-1587217 Share on other sites More sharing options...
kicken Posted June 14, 2021 Share Posted June 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312905-very-very-simple-code-question/#findComment-1587218 Share on other sites More sharing options...
TechnoDiver Posted June 14, 2021 Author Share Posted June 14, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/312905-very-very-simple-code-question/#findComment-1587219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.