0

Okay, I'm not a complete beginner in php but I don't understand why is some tutorial I was reading creating a taxonomy in wordpress used double quote's and in the wordpress codex it use's single quotes.

Now my understanding of single and double quotations works like this

<?php

//double quotes
 $colour = brown;
  echo "The dog has $colour fur.";
//outputs - the dog has brown fur.

//single quotes 
 $colour = brown;
  echo 'the dog has $colour fur.';
//outputs - the dog has $colour fur.

?>

But what I don't quite understand is this.

register_taxonomy("project-type",
 array("portfolio"),
 array("hierarchical" => true, 
"label" => "Project Types",
 "singular_label" => "Project Type", 
"rewrite" => true));

what difference does it make if its inside an array? and on the wp codex is use's single quotes like so.

add_action( 'init', 'create_book_tax' );

function create_book_tax() {
    register_taxonomy(
        'genre',
        'book',
        array(
            'label' => __( 'Genre' ),
            'rewrite' => array( 'slug' => 'genre' ),
            'hierarchical' => true,
        )
    );
}
user2478101
  • 161
  • 7
  • Take a look at http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – BMN Jul 26 '13 at 10:30
  • It does not make any difference in cases like that. People use whatever suits them better. *Theoretically* single quotes have better performance, but in practice no difference is measurable. – Jon Jul 26 '13 at 10:30
  • Absolutely no difference. Its good coding practice to only use double quotes if you actually want to make use of PHP's ability. That makes the code almost self documenting. – RiggsFolly Jul 26 '13 at 10:31
  • This is wrong `$colour = brown;` string must be enclosed in quotes either single or double `$colour = 'brown';` – Khawer Zeshan Jul 26 '13 at 10:31
  • @RiggsFolly : There is a difference ! When outputting `echo '$foo'` vs echo `"$foo"` !! – DarkBee Jul 26 '13 at 10:33
  • @DarkBee Yes of course there is a difference them, but the question was 'what difference does it make if its inside an array?' Not is there a difference, he already described that difference in his question. – RiggsFolly Jul 26 '13 at 11:08

3 Answers3

3

The only difference between single quotes and double quotes is that strings in double quotes get parsed for variables and control characters.

So:

$var = 'test';
echo "$var";

Will print test

While:

$var = 'test';
echo '$var';

Will print $var

Control characters like \n (newline) will also only work in double quotes.

Because strings in double quotes are being parsed, using double quotes is actually slightly slower performance wise too (though hardly noticable).

So basically its simply about strings. The string being part of an array or not is actually irrellevant.

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55
0

Things get evaluated in double quotes but not in single:

Eg: 1

$s = "dollars";

echo 'This costs a lot of $s.';

Output

  This costs a lot of $s.

Eg: 2

echo "This costs a lot of $s."; 

Output

 This costs a lot of dollars.
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
0

To sum it up: It doesn't make a difference in your example. We don't know why the author chose " over ', maybe personal preference, or it's easier to type in his keyboard scheme, or ...

Langdi
  • 229
  • 1
  • 10