Bird
Raised Fist0
PHPprogramming~10 mins

String concatenation operator in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String concatenation operator
Start
↓
Two strings
↓
Use . operator
↓
Combine strings
↓
Result: concatenated string
↓
End
This flow shows how two strings are joined using the dot (.) operator to create one combined string.
Execution Sample
PHP
<?php
$a = "Hello";
$b = " World";
$c = $a . $b;
echo $c;
?>
This code joins two strings "Hello" and " World" using the dot operator and prints the result.
Execution Table
StepActionVariableValueOutput
1Assign string$a"Hello"
2Assign string$b" World"
3Concatenate $a and $b$c"Hello World"
4Print $cOutputHello World
💡 End of script after printing concatenated string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$aundefined"Hello""Hello""Hello""Hello"
$bundefinedundefined" World"" World"" World"
$cundefinedundefinedundefined"Hello World""Hello World"
Key Moments - 2 Insights
Why do we use the dot (.) instead of plus (+) to join strings in PHP?
In PHP, the dot (.) is the operator for joining strings. Using plus (+) tries to add numbers, so it won't join strings. See step 3 in the execution_table where $c is assigned using the dot.
What happens if one of the variables is not a string?
PHP converts non-string values to strings when using the dot operator. So concatenation still works, but the value is converted first.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $c after step 3?
A" World"
B"Hello"
C"Hello World"
Dundefined
💡 Hint
Check the 'Value' column for $c at step 3 in the execution_table.
At which step is the output produced?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the 'Output' column in the execution_table to find when output appears.
If $b was assigned the number 5 instead of a string, what would $c be after concatenation?
A"Hello5"
B"Hello World"
CError
D"Hello 5"
💡 Hint
Recall that PHP converts numbers to strings when using the dot operator, as explained in key_moments.
Concept Snapshot
PHP uses the dot (.) operator to join strings.
Syntax: $combined = $str1 . $str2;
It converts non-strings to strings automatically.
Plus (+) is NOT for strings in PHP.
Result is a new combined string.
Full Transcript
This visual trace shows how PHP joins two strings using the dot operator. First, variables $a and $b get string values. Then $c is assigned by joining $a and $b with the dot. Finally, $c is printed, showing the combined string. The dot operator is special for strings in PHP and converts other types if needed. The output appears only after printing $c. This helps beginners see each step clearly.

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