OM2 Posted July 27, 2008 Share Posted July 27, 2008 Can someone tell me what's wrong with the following: $myNewArray = array(); $myNewArray[variable1] = "Hello world"; $myNewArray[variable2] = "Hello Universe"; $myNewArray[weirdName] = "Hello Everyone!"; If I have error_reporting(E_ALL); on the first line, this throws many errors. The errors say something like: Notice: Use of undefined constant variable1 - assumed 'variable1' in /home/user/public_html/dirs/dir2/dir3/myTest.php on line 5 I'm a bit lost, I didn't think there was anything wrong with my array declreation. If I leave error_reporting(E_ALL); out, then all is OK! But because it's throwing errors, I guess I must be doing something semantically wrong? Thanks. OM Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/ Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 Try doing: $myNewArray = array(); $myNewArray['variable1'] = "Hello world"; $myNewArray['variable2'] = "Hello Universe"; $myNewArray['weirdName'] = "Hello Everyone!"; The error indicates that PHP is looking for a constant defined earlier titled variable1 because variable1 is not defined in quotes inside your array arguement that is declaring it. Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/#findComment-600858 Share on other sites More sharing options...
.josh Posted July 27, 2008 Share Posted July 27, 2008 when you do $blah[something] php first checks to see if something is a constant. Since you did not make it a constant, it's throwing you that error. However, if php fails to find a constant, it will treat it will then attempt to use it as a string like $blah['something']. So...your code will work as intended, but you get that message. To fix it, use the quotes around it as njoj pointed out. Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/#findComment-600863 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 aah... ok. i will do this now. just to confirm though, if i have: $myNewArray = array(); $myNewArray['variable1'] = "Hello world"; $myNewArray['variable2'] = "Hello Universe"; $myNewArray['weirdName'] = "Hello Everyone!"; when i actually want to reference the items, should i use $myNewArray[weirdName] or $myNewArray['weirdName']? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/#findComment-600891 Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 With the quotes would be best, you would probably get a warning similar to the original one if you did it without the quotes because PHP looks to see if you defined any constant called weirdName earlier in the script. Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/#findComment-600893 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 guys: thanks. i'm marking this thread as solved. Quote Link to comment https://forums.phpfreaks.com/topic/116848-solved-newbie-need-help-declaring-arrays/#findComment-600894 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.