Strings hold text in programs. PHP lets you write strings with single or double quotes. They work a bit differently, so knowing when to use each helps you write clearer code.
String type (single vs double quotes) in PHP
$single_quoted = 'Hello, world!'; $double_quoted = "Hello, world!";
Single quotes treat the text mostly as plain text.
Double quotes allow variables and special characters like \n (new line) to work inside the string.
$greeting1, the variable $name is not replaced because single quotes treat it as plain text.In
$greeting2, the variable $name is replaced with its value because double quotes allow variable parsing.$name = 'Alice'; $greeting1 = 'Hello, $name!'; $greeting2 = "Hello, $name!";
$text1, the \n is shown as two characters \ and n.In
$text2, the \n creates a new line.$text1 = 'Line1\nLine2'; $text2 = "Line1\nLine2";
Double quotes need backslash to escape double quotes inside.
$quote1 = 'It\'s easy!'; $quote2 = "She said, \"Hello!\"";
This program shows how single and double quotes handle variables and new lines differently.
<?php $name = "Bob"; // Using single quotes echo 'Hello, $name!\n'; // Using double quotes echo "Hello, $name!\n"; // Showing new lines echo 'First line\nSecond line\n'; echo "First line\nSecond line\n"; ?>
Use single quotes when you want the string exactly as you type it, without variable replacement.
Use double quotes when you want variables inside the string to be replaced with their values.
Remember to escape quotes inside strings if they match the surrounding quotes.
Single quotes treat the string as plain text.
Double quotes allow variables and special characters like new lines.
Choose quotes based on whether you want variable replacement or not.
Practice
Solution
Step 1: Understand single quotes behavior
Single quotes in PHP treat the string literally, so variables inside are not replaced.Step 2: Understand double quotes behavior
Double quotes allow variable replacement and special characters like \n for new lines.Final Answer:
Single quotes treat the string as plain text without variable replacement. -> Option AQuick Check:
Single quotes = plain text [OK]
- Thinking single quotes allow variable replacement
- Assuming double quotes behave like single quotes
- Believing both quotes behave the same
Solution
Step 1: Check variable replacement in quotes
Variables inside double quotes get replaced with their values automatically.Step 2: Analyze each option
echo 'Hello $name'; uses single quotes, so $name is not replaced. echo 'Hello ' . $name . ';'; concatenates correctly but is not the direct variable inside string. echo Hello $name; is invalid syntax.Final Answer:
echo "Hello $name"; -> Option CQuick Check:
Double quotes allow variable replacement [OK]
- Using single quotes expecting variable replacement
- Missing quotes around strings
- Incorrect concatenation syntax
$name = "Alice"; echo 'Hello $name\n';
Solution
Step 1: Understand single quotes behavior
Single quotes treat the string literally, so variables and escape sequences like \n are not processed.Step 2: Analyze the output
The string prints exactly as written: Hello $name\n (with backslash and n characters).Final Answer:
Hello $name\n -> Option AQuick Check:
Single quotes = no variable or escape processing [OK]
- Expecting variable replacement inside single quotes
- Expecting \n to create a new line inside single quotes
- Confusing single and double quotes behavior
$age = 25; echo 'I am $age years old.';
Solution
Step 1: Check variable replacement in single quotes
Variables inside single quotes are not replaced, so $age prints literally.Step 2: Check syntax and variable definition
There is no syntax error and $age is defined, so no runtime error.Final Answer:
It will print: I am $age years old. (no variable replacement). -> Option BQuick Check:
Single quotes = no variable replacement [OK]
- Assuming variables inside single quotes get replaced
- Thinking missing semicolon causes error here
- Confusing runtime error with variable usage
Solution
Step 1: Check variable replacement and newline support
Double quotes allow variable replacement and interpret escape sequences like \n as newline.Step 2: Analyze each option
echo 'User: $user\n'; uses single quotes, so no variable replacement or newline. echo 'User: ' . $user . '\n'; concatenates but \n inside single quotes is literal, so no newline. echo "User: $user\"; has a syntax error due to unescaped double quote.Final Answer:
echo "User: $user\n"; -> Option DQuick Check:
Double quotes = variable + newline support [OK]
- Using single quotes expecting newline or variable replacement
- Incorrect escaping of quotes inside strings
- Concatenating but forgetting newline is literal in single quotes
Ready to prove it?
Pass the test and this pattern moves unknown
