Free PHP course. Sign Up for tracking progress →

PHP: Quotes

'Hello'
'Goodbye'

'G'
' '
''

Which of these five items are strings?

With the first two, everything is clear, they're strings, we've already worked with similar constructions and said that strings are sets of characters.

Any single character between parentheses is a string. '', is also a string (albeit empty). So everything inside the quotation marks can be considered a string, even if it's a space, one character, or no characters at all.

In previous lessons, we enclosed strings in single quotes. You can also use double quotes:

<?php

print_r("Dracarys!");

Imagine you want to type the line dragon's mother. The apostrophe before the letter s and a single quote are the same symbols. Let's print it:

<?php

print_r('Dragon's mother');
// PHP Parse error: syntax error, unexpected 's' (T_STRING), expecting ',' or ')'

This program won't work. From PHP's perspective, the line started with a single quote and then ended after the letter n. Then comes some characters: s mother without quotes, which are not a string. And then there's one quote mark that opens a string which is never closed: ');. This code is syntactically incorrect (you can see it by the way the code is highlighted).

It's a good idea here to use double quotes. This version of the program will work correctly:

<?php

print_r("Dragon's mother");

Now the interpreter knows that the string started with a double quote, so it must end with a double quote. And the single quote inside has become the part of the string.

It works the other way too. If you want to use double quotes inside a string, you should enclose the string in single quotes. And the number of quote marks inside the string itself doesn't matter.

Now, what if we want to create a string like this?

Dragon's mother said "No"

It has both single and double quotes. What can we do in this case? We need to somehow tell the interpreter to consider each quote as part of the string, not the beginning or the end.

To do this, you need to use an escape character. In our case, it's the character that marks the start and end of a string, either a single or double quote, depending. Use a backslash \ before the character you want to escape.

<?php

// Only escape ", since in this situation
// double quotes have a special meaning
print_r("Dragon's mother said \"No\"");
// => Dragon's mother said "No"

Look closely: we had to use \ for double quotes to escape them, and not for the single quote (apostrophe) because the string is written in double quotes. If the string were written in single quotes, the escape character would be used before the apostrophe, not before the double quotes.

<?php

// \ is displayed if it's followed by a normal character, and
// not a special one
print_r("Death is \so terribly final");
// => Death is \so terribly final

But what if you want to print the backslash? Just like any other special symbol, it escapes using a backslash too.

<?php

print_r("\\");
// => \

Self-test: what will be printed?

<?php

print_r("\\ \\ \\\\ \\\ \'\"");

Instructions

Write a program that prints:

"Khal Drogo's favorite word is "athjahakar""

The program should display this exact phrase on the screen. Note the quotes at the beginning and the end of the phrase:

"Khal Drogo's favorite word is "athjahakar""
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community