thtmal Posted February 10, 2008 Share Posted February 10, 2008 associative array examples that I found on http://www.webcheatsheet.com/php/arrays.php part of the 1st example(echo each associative array separately): echo $second_array['key4']; part of the 2nd example(echo by combining strings and each associative array): echo "rose costs $flower_shop[rose], daisy costs $flower_shop[daisy], and orchid costs $flower_shop[orchid]."; I'm using PHP 5.2.5. The 1st example shows to echo each associative array separately, must use the single quote ['key1'], but with the 2nd example, the single quote causes an error if used, and must be removed. Does this have to do with the different versions of PHP, or is this the way PHP always worked. Because from the a few of the negative articles that I've read about PHP, is that it is a sloppy language(I'm not putting it down personally, it's just what I've read), and if that's just the way PHP worked as always, then I'm just starting to see maybe why. I used to program in VB, and do not recall the VB language being like this, in terms of using the syntax in an inconsistent way. Just curious Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/ Share on other sites More sharing options...
pocobueno1388 Posted February 10, 2008 Share Posted February 10, 2008 I don't know exactly why it causes an error, but you can simply wrap curly braces around the variable to avoid it. echo "rose costs {$flower_shop['rose']}, daisy costs {$flower_shop['daisy']}, and orchid costs {$flower_shop['orchid']}."; Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463039 Share on other sites More sharing options...
roopurt18 Posted February 10, 2008 Share Posted February 10, 2008 You should always use single quotes around associative array keys (unless they are defined as a constant of course). echo $array['someKey']; If you are going to embed variables in double quoted strings, then you should always enclose them in curly brackets, whether or not they are arrays. $var = 'Hello'; $var2 = Array( 'msg' => 'World' ); echo "{$var}, {$var2['msg']}"; These are what I would consider to be "best practices," as they help avoid errors from sloppy coding. BTW, I'm not sure what the exact reason for the error is; at first I was inclined to say it was a parsing error but I'm not so sure that would be the case. It doesn't matter though if you follow the best practices I outlined above. Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463052 Share on other sites More sharing options...
PFMaBiSmAd Posted February 10, 2008 Share Posted February 10, 2008 The syntax error caused by putting single-quotes around index string names is because the php parser could be written better. When you use single quotes inside of the double-quoted string, they are treated as literal single-quote characters and have no special meaning. The parser basically finds an unexpected character and cannot figure out that you have an array index enclosed in single-quotes. Leaving the single-quotes out allows the parser to figure out a solution (the defined constant vs single-quoted index string name assumption logic results in the the correct parsing of the variable in the string.) Adding the {} around the variable does two things. It allows consistent syntax and it forces the parser into a "variable parsing" mode where it correctly figures out what to do. Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463057 Share on other sites More sharing options...
thtmal Posted February 10, 2008 Author Share Posted February 10, 2008 Thanks to both for the replies and suggestions, much appreciated. I understand that consistent coding make less sloppy scripts. I used to write a lot in VB and always kept good comments and kept my codes nice and neat. Just curious about PHP, see below array example $flower_shop = array ( "rose" => "5.00", 'daisy' => '4.00', "orchid" => '2.00', 'tulip' => "1.00" ); The double quotes work, single quote also works or mix and match. It's like PHP was designed without a solid set of rules, and can use different ways of doing the exact same thing. Maybe that's why SOME of the articles I've read considers the PHP language as sloppy. I guess it'll just take a little of getting used to. Is Perl different in this respect? Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463059 Share on other sites More sharing options...
PFMaBiSmAd Posted February 10, 2008 Share Posted February 10, 2008 Those are not different ways of doing the same thing. In that example the results are the same but there is a technical difference between single and double quotes. Learning more of the php language before questioning or complaining about some aspect of it will help - http://php.net/string Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463066 Share on other sites More sharing options...
thtmal Posted February 10, 2008 Author Share Posted February 10, 2008 Thanks PF, now I see what the differences are, will refer to the php site manual as much as I can. Well, it has been almost 8 yrs since I wrote any type of programs, the final one was a Windows app that I licensed to Kraft Foods and Volvo, etc to run on their hundreds to thousands of networked nodes at their plants. Quote Link to comment https://forums.phpfreaks.com/topic/90304-associative-array-single-quote-question/#findComment-463071 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.