Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,048
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Some questions about Associative Arrays

    Okay, I've got some dumb questions about Associative Arrays...

    (And, yes, I looked in the Manual, but it wasn't very clear!)


    Questions:


    Based on this code...
    PHP Code:
    $sectionInfo = array("sectionName" => "$sectionName""sectionHeading" => "$sectionHeading"); 
    1.) When I create an Associative Array, do I have to wrap the Keys in quotes?

    2.) When I create an Associative Array, do I have to wrap the Values in quotes?

    3.) If yes to either of these, does it matter if I use Single Quotes or Double Quotes?


    Based on this code...
    PHP Code:
    echo $sectionInfo['sectionHeading'
    4.) When I call an Associative Array, must I wrap the Key in quotes?

    5.) If yes, does it matter if I use Single Quotes or Double Quotes?

    Sincerely,


    Debbie

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,283
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by doubledee View Post
    Questions:


    Based on this code...
    PHP Code:
    $sectionInfo = array("sectionName" => "$sectionName""sectionHeading" => "$sectionHeading"); 
    1.) When I create an Associative Array, do I have to wrap the Keys in quotes?
    Only if its a string. If it's a variable then no, just put $Variable there as the key without quotes.

    Quote Originally Posted by doubledee View Post
    2.) When I create an Associative Array, do I have to wrap the Values in quotes?
    As above. Only if its a hard coded string. If its a $variable you don't need to UNLESS the $variable is within the string - in that case double quotes not single.

    Quote Originally Posted by doubledee View Post
    3.) If yes to either of these, does it matter if I use Single Quotes or Double Quotes?
    Single quotes are literal. Double quotes are magical (Easy way to remember this is that a double quote has two marks and so has twice the power - magic!).

    By this I mean:
    PHP Code:
    $Var 'John Smith';

    echo 
    'Name: $Var'// Name: $Var
    echo "Name: $Var"// Name: John Smith 
    Single quotes print as-is. Double quotes use php magic to replace variables with their values. Double quotes also use extra CPU resources so if you're using a hard coded string, use single quotes.

    Quote Originally Posted by doubledee View Post
    Based on this code...
    PHP Code:
    echo $sectionInfo['sectionHeading'
    4.) When I call an Associative Array, must I wrap the Key in quotes?
    Yes. Again if its a hard coded string, single quotes. Doubles if its a string containing a variable. If its a variable by itself then no quotes are needed within the square brackets.

    Quote Originally Posted by doubledee View Post
    5.) If yes, does it matter if I use Single Quotes or Double Quotes?
    See previous reply or the link in my signature about quotes.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,048
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Quote Originally Posted by doubledee
    Based on this code...
    PHP Code:
    $sectionInfo = array("[COLOR="Red"]sectionName[/COLOR]" => "$sectionName""[COLOR="Red"]sectionHeading[/COLOR]" => "$sectionHeading"); 
    1.) When I create an Associative Array, do I have to wrap the Keys in quotes?
    Only if its a string. If it's a variable then no, just put $Variable there as the key without quotes.
    That makes no sense.

    In the above example, the "KEY" - shown in red - is never going to be a variable.


    Quote Originally Posted by tangoforce View Post
    Quote Originally Posted by doubledee
    3.) If yes to either of these, does it matter if I use Single Quotes or Double Quotes?
    Single quotes are literal. Double quotes are magical (Easy way to remember this is that a double quote has two marks and so has twice the power - magic!).
    Right, that much I know.

    But in the context of an Array, and more specifically referencing an Array's Key, does using Single versus Double Quotes matter?

    (I have almost always seen people use Single Quotes.)

    Sincerely,


    Debbie

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,283
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by doubledee View Post
    That makes no sense.

    In the above example, the "KEY" - shown in red - is never going to be a variable.
    Yes it does (if you're using a variable anyway), you're just not seeing what I'm getting at.

    Suppose you want to store a mysql row in your session for instance (and I literally posted this in another thread earlier today):

    PHP Code:
    //$Row = mysql_fetch_array($Result);

    foreach($Row as $Key => $Value)
       {
       
    $_SESSION[$Key] = $Value;
       } 

    That has just dynamically used a variable as an associative key. So YES, the key CAN be a variable (maybe not in your code but plenty of us do use this technique).


    Quote Originally Posted by doubledee View Post
    Right, that much I know.
    Then all you need to know is that the same applies inside the square brackets. Single quotes for hard coded literal strings, double quotes for anything that may hae a variable in it and no quotes at all for a variable by itself.

    Quote Originally Posted by doubledee View Post
    But in the context of an Array, and more specifically referencing an Array's Key, does using Single versus Double Quotes matter?
    Only if you're using a variable. Look at this:
    PHP Code:
    //Best way
    $_SESSION['test'] = 'this';
    $Array = array('test' => 'this');

    //This is fine but will eat more CPU as php examines the double quoted string
    $_SESSION["test"] = "this";
    $Array = array("test" => "this");

    //Error - PHP may ignore if you don't have full error reporting turned on
    $_SESSION[test] = 'this';
    $Array = array(test => 'this');

    //Variables
    $Var 'test';
    $_SESSION[$Var] = 'this';
    $_SESSION["Now $Var"] = 'this'//$_SESSION["Now test"] = 'this';
    $Array = array($Var => 'this');
    $Array = array("Now $Var" => 'this'); 
    Quote Originally Posted by doubledee View Post
    (I have almost always seen people use Single Quotes.)
    Yes, if it's a hard coded string / key then yes single quotes are the most appropriate (remember that an associative array key is basically a string).

    All you need to remember is that when setting your key if it's not numeric, then you treat it as a normal string and the same quote mark rules apply.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    doubledee (05-26-2013)

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,048
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Yes it does (if you're using a variable anyway), you're just not seeing what I'm getting at.

    Suppose you want to store a mysql row in your session for instance (and I literally posted this in another thread earlier today):

    PHP Code:
    //$Row = mysql_fetch_array($Result);

    foreach($Row as $Key => $Value)
       {
       
    $_SESSION[$Key] = $Value;
       } 

    That has just dynamically used a variable as an associative key. So YES, the key CAN be a variable (maybe not in your code but plenty of us do use this technique).
    Fair enough.


    Only if you're using a variable. Look at this:
    [php]
    //Best way
    $_SESSION['test'] = 'this';
    $Array = array('test' => 'this');

    //This is fine but will eat more CPU as php examines the double quoted string
    $_SESSION["test"] = "this";
    $Array = array("test" => "this");

    //Error - PHP may ignore if you don't have full error reporting turned on
    $_SESSION[test] = 'this';
    $Array = array(test => 'this');
    The last one is the one I was running into.

    I thought it was legal syntax, but now I see it isn't.

    My adjusted code looks like this...
    PHP Code:
        // Build Array.
        
    $sectionInfo = array('sectionName' => $sectionName'sectionTagline' => $sectionTagline); 

    And...
    PHP Code:
        $sectionInfo getSectionInfo($dbc$sectionSlug);
        
    $sectionName $sectionInfo['sectionName'];
        
    $sectionTagline $sectionInfo['sectionTagline']; 


    Yes, if it's a hard coded string / key then yes single quotes are the most appropriate (remember that an associative array key is basically a string).

    All you need to remember is that when setting your key if it's not numeric, then you treat it as a normal string and the same quote mark rules apply.
    Okay, thanks for the help!!!

    Sincerely,


    Debbie

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,283
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    No worries, glad to be of service
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,216
    Thanks
    12
    Thanked 338 Times in 334 Posts
    PHP Code:
    //Error - PHP may ignore if you don't have full error reporting turned on
    $_SESSION[test] = 'this';
    $Array = array(test => 'this'); 
    The last one is the one I was running into.

    I thought it was legal syntax, but now I see it isn't.
    it is legal syntax, if you use it right. the test in the code is seen by PHP as the constant test, which in itself is a legal identifyer. but if you meant it to be the string "test" and do not have such a constant defined, PHP will issue an "undefined constant, assuming string" notice,
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •