Jump to content

Associative Array Single Quote Question?


thtmal

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.