PHP PHP Basics Getting to Know PHP Intro to Workspaces and PHP Code

Single or doble quotation marks

When you write the code <?php echo "Hello World!"; ?> or <?php echo 'Hello World!'; ?> Why Alena Holligan used single quotation marks? or Can you use doble quotation marks? any difference.

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

Hi Oscar! In this case, yes, you could use either single or double quotes. Either work for this example. The biggest difference is when you have a string containing a variable name in it. Let me show you an example and feel free to run this in a workspace!

 $hello = "Hello World!";
 echo "$hello";
 echo '$hello';

If/when you run this code the first echo will actually echo out "Hello World!". It expands the variable to retrieve the value inside it and display the value. The single quotes don't do that. They simply echo out the variable name. So the second echo will echo out $hello.

She could have used either here and achieved the same results. But she had to choose one, and the single quotes is what she chose for this example. Hope this helps! :sparkles:

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

Best answer assigned 2017-07-06 as no other answers are present and answer has been upvoted multiple times.