Bird
Raised Fist0
PHPprogramming~5 mins

String concatenation operator in PHP - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the string concatenation operator in PHP?
The string concatenation operator in PHP is the dot (.) symbol. It joins two or more strings together.
Click to reveal answer
beginner
How do you join the strings "Hello" and "World" in PHP?
You use the concatenation operator like this: $greeting = "Hello" . " " . "World"; which results in "Hello World".
Click to reveal answer
beginner
What will be the output of this PHP code?
$a = "Good";
$b = "Morning";
echo $a . " " . $b;
The output will be: Good Morning. The dot operator joins the strings and the space " " adds a space between them.
Click to reveal answer
beginner
Can you use the plus (+) operator to concatenate strings in PHP?
No, the plus (+) operator is for numbers only. Using it with strings will cause errors or unexpected results. Use the dot (.) operator for strings.
Click to reveal answer
beginner
How to concatenate multiple strings in PHP in one statement?
You can chain the dot operator like this:
$full = "I" . " " . "love" . " " . "PHP";
This results in "I love PHP".
Click to reveal answer
Which operator is used to join strings in PHP?
A- (minus)
B+ (plus)
C& (ampersand)
D. (dot)
What will this PHP code output?
echo "Hi" . " " . "there!";
AHi+there!
BHi there!
CHiThere!
DError
What happens if you use + to join strings in PHP?
AIt concatenates strings
BIt converts strings to uppercase
CIt adds numbers only, strings cause error or unexpected results
DIt repeats the string
How to add a space between two concatenated strings in PHP?
AUse a space inside one string or concatenate a space string like " "
BUse + operator
CUse a comma between strings
DUse a semicolon
Which of these is a valid PHP string concatenation?
A$x = "a" . "b" . "c";
B$x = "a" + "b" + "c";
C$x = "a" & "b" & "c";
D$x = "a" - "b" - "c";
Explain how to join two strings in PHP and how to include a space between them.
Think about the symbol used to join strings and how to add spaces.
You got /4 concepts.
    Describe why the plus (+) operator is not used for string concatenation in PHP.
    Consider the difference between numbers and strings in PHP operators.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the dot (.) operator do in PHP?
      easy
      A. Joins two strings together
      B. Adds two numbers
      C. Compares two strings
      D. Divides two numbers

      Solution

      1. Step 1: Understand the dot operator usage

        In PHP, the dot operator is used to join or concatenate strings.
      2. Step 2: Differentiate from other operators

        It does not perform arithmetic or comparison; it only combines strings.
      3. Final Answer:

        Joins two strings together -> Option A
      4. Quick Check:

        Dot operator = string join [OK]
      Hint: Dot (.) always joins strings in PHP [OK]
      Common Mistakes:
      • Confusing dot with plus (+) for addition
      • Thinking dot compares strings
      • Using dot for division
      2. Which of the following is the correct way to concatenate two strings $a and $b in PHP?
      easy
      A. $a + $b
      B. $a . $b
      C. $a & $b
      D. $a * $b

      Solution

      1. Step 1: Identify the concatenation operator

        PHP uses the dot (.) operator to join strings, so $a . $b is correct.
      2. Step 2: Check other operators

        Plus (+) adds numbers, ampersand (&) is bitwise AND, and asterisk (*) is multiplication.
      3. Final Answer:

        $a . $b -> Option B
      4. Quick Check:

        Dot (.) concatenates strings [OK]
      Hint: Use dot (.) to join strings, not plus (+) [OK]
      Common Mistakes:
      • Using + instead of . for strings
      • Using & or * operators mistakenly
      • Missing the dot operator
      3. What is the output of the following PHP code?
      $greeting = "Hello";
      $name = "Alice";
      echo $greeting . ", " . $name . "!";
      medium
      A. Hello,Alice!
      B. Hello Alice!
      C. Hello, Alice!
      D. Hello . , . Alice . !

      Solution

      1. Step 1: Analyze string concatenation

        The code joins "Hello", ", ", "Alice", and "!" using the dot operator.
      2. Step 2: Combine strings step-by-step

        "Hello" . ", " = "Hello, " and then "Hello, " . "Alice" = "Hello, Alice" and finally "Hello, Alice" . "!" = "Hello, Alice!"
      3. Final Answer:

        Hello, Alice! -> Option C
      4. Quick Check:

        Concatenate with dot = Hello, Alice! [OK]
      Hint: Dot joins strings exactly as written [OK]
      Common Mistakes:
      • Missing spaces inside strings
      • Expecting plus (+) to work
      • Confusing output with literal dots
      4. Find the error in this PHP code:
      $first = "Good";
      $second = "Morning";
      echo $first + " " + $second;
      medium
      A. Using double quotes incorrectly
      B. Missing semicolon after echo
      C. Variables not declared
      D. Using + instead of . for string concatenation

      Solution

      1. Step 1: Identify operator misuse

        The code uses + to join strings, but PHP requires the dot (.) operator for concatenation.
      2. Step 2: Confirm other syntax is correct

        Semicolons are present, variables declared, and quotes are correct.
      3. Final Answer:

        Using + instead of . for string concatenation -> Option D
      4. Quick Check:

        Use dot (.) not plus (+) for strings [OK]
      Hint: Remember: + is for numbers, . is for strings [OK]
      Common Mistakes:
      • Using + to join strings
      • Thinking + works like in JavaScript
      • Ignoring PHP string rules
      5. You want to create a full name string from $firstName and $lastName with a space between them. Which code correctly does this in PHP?
      hard
      A. $fullName = $firstName . " " . $lastName;
      B. $fullName = $firstName + " " + $lastName;
      C. $fullName = $firstName . $lastName;
      D. $fullName = '$firstName $lastName';

      Solution

      1. Step 1: Understand string concatenation with space

        To join first and last names with a space, you must add " " explicitly between them using dot operators.
      2. Step 2: Evaluate each option

        A misses the space, B uses + which is invalid for strings, D uses single quotes so variables are not interpolated.
      3. Final Answer:

        $fullName = $firstName . " " . $lastName; -> Option A
      4. Quick Check:

        Dot with space string joins names correctly [OK]
      Hint: Add space as " " between strings with dot [OK]
      Common Mistakes:
      • Forgetting space between names
      • Using + instead of .
      • Assuming variables parse inside single quotes
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard