Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Functions Introducing Functions PHP Function Arguments

見綸 陳
見綸 陳
6,333 Points

Different between " " and ' ' ? And which one is better?

At video 2:00, teacher talk about " " and ' '.

I want to know what different between " " and ' ' in php programming?

I do couple test in my localhost to run index.php.

Here is my code example. Can I say " " is better than ' ' ?

        <!-- Pracetice function argument -->
        <?php
        function welcome_dialog($user_array) {
            if(is_array($user_array)) {
                foreach($user_array as $name) {
                    echo "Hello, $name, how are you?</br>"; // case 1 --> is work
                    echo 'Hello, $name, how are you?</br>'; // case 2 --> isn't work
                    echo 'Hello,'.$name.', '.'how are you?</br>'; // case 3 --> is work
                    echo "Hello,".$name.", "."how are you?</br>"; // case 4 --> is work
                }
            } else {
                echo 'Hello friends</br>';
            }
        }

        $name = array(
            'Kcin',
            'Sharon',
            'Brother',
            'Sister'
        );

        welcome_dialog($name);

        ?>

2 Answers

Matthew Smart
Matthew Smart
12,567 Points

Both are exactly the same thing. It does not matter which one you use. However i will demonstrate how you would use both together:

  $div = "<div class='class-name'> <h1>Hello world</h1> </div>";

As you can see where it says class='class-name' if I was to use "" it would have stopped the php.

So you can use either one, but if you need to do another quotes inside of an already open php string, make sure they are different.

Bad examples

 $div = "<div class="class-name"> <h1>Hello world</h1> </div>";
 $div = '<div class='class-name'> <h1>Hello world</h1> </div>';

Good examples

 $div = "<div class='class-name'> <h1>Hello world</h1> </div>";
 $div = '<div class="class-name"> <h1>Hello world</h1> </div>';

In addiiton,

You can use the same quotes to wrap a string if you escape the quotes within a string (using a backslash):

<?php

 $div = "<div class=\"class-name\"> <h1>Hello world</h1> </div>";

There is a difference between the two types of quotes:

Single quotes don't do anything special, but double quotes (") are used for string interpolation. This basically means variables inside double quotes are parsed, and their values printed. e.g.

<?php

$name = 'Tom';

echo 'My name is $name'; // My name is $name

echo "My name is $name"; // My name is Tom

It's sometimes nicer to use this method instead of loads of string concatenation.

<?php

$variable1 = 'thing';
$variable2 = 'thing';
$variable3 = 'thing';

$someLongString = 'My favourite things are '. $variable1 .', '. $variable2 .' and '. $variable3;

// could become

$someLongString = "My favourite things are $variable1, $variable2 and $variable3";

See php strings for more information.

Thank you both for your answers on this. To clarify further, Matthew Smart's answer above is a good answer which provides needed context on using single and double quotes with strings in PHP.

Tom Cawthorn's answer above actually addresses and explains the reason and rationale for why Hampton changed his code from 'Hello, $name!' to "Hello, $name!"

It was because bounding the characters in single quotes will not cause the string to be interpolated, but bounding the characters in double quotes will cause the string to be interpolated, thus returning the value of the variable in the web page.