jaunty_mellifluous Posted September 11, 2008 Share Posted September 11, 2008 I'm fairly new to php, trying to learn it from a book. They've given me some example code to let me understand how arrays are assigned. <?php function assign_key() { return 'd'; } $foo['a'] = 1; $foo['b'] = 2; $foo['c'] = 3; $foo[assign_key()] = 4; /*Assigned the key value 'd' */ ?> This is the Code. But I'm getting "Cannot use a scalar value as an array" errors when I load the page in browser. What does it mean and what's the problem with the code? Thanks. Link to comment https://forums.phpfreaks.com/topic/123794-cannot-use-a-scalar-value-as-an-array-error/ Share on other sites More sharing options...
BlueSkyIS Posted September 11, 2008 Share Posted September 11, 2008 is that all of the code? it works for me: <?php function assign_key() { return 'd'; } $foo['a'] = 1; $foo['b'] = 2; $foo['c'] = 3; $foo[assign_key()] = 4; /*Assigned the key value 'd' */ print_r($foo); ?> output: Array ( [a] => 1 => 2 [c] => 3 [d] => 4 ) Link to comment https://forums.phpfreaks.com/topic/123794-cannot-use-a-scalar-value-as-an-array-error/#findComment-639184 Share on other sites More sharing options...
jaunty_mellifluous Posted September 11, 2008 Author Share Posted September 11, 2008 Hmm, then maybe it's the php settings on program I'm using to run php. I'm using Wamp Server. Any solutions how I can solve this? Link to comment https://forums.phpfreaks.com/topic/123794-cannot-use-a-scalar-value-as-an-array-error/#findComment-639186 Share on other sites More sharing options...
jaunty_mellifluous Posted September 11, 2008 Author Share Posted September 11, 2008 Ok it works. I was using it on a page where I had initialised $foo before. hehe. Link to comment https://forums.phpfreaks.com/topic/123794-cannot-use-a-scalar-value-as-an-array-error/#findComment-639188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.