📖 Scenario: You are creating a simple PHP script to combine parts of a greeting message. This is common when you want to build sentences from smaller pieces.
🎯 Goal: Build a PHP script that uses the string concatenation operator to join parts of a greeting message and display the full message.
📋 What You'll Learn
Create variables with exact string values
Use the string concatenation operator . to join strings
Print the final concatenated string
💡 Why This Matters
🌍 Real World
Combining strings is useful for building messages, file paths, or any text output dynamically.
💼 Career
Understanding string concatenation is essential for web developers working with PHP to create dynamic web pages.
Progress0 / 4 steps
1
Create greeting parts
Create two string variables called $greeting with the value "Hello" and $name with the value "World".
PHP
Hint
Use = to assign strings to variables. Strings must be in double quotes.
2
Create a space variable
Create a string variable called $space and set it to a single space character " ".
PHP
Hint
Remember a space is just a string with one space character inside quotes.
3
Concatenate strings
Create a variable called $message that concatenates $greeting, $space, and $name using the string concatenation operator ..
PHP
Hint
Use the dot . operator between each variable to join them.
4
Print the message
Use echo to print the variable $message.
PHP
Hint
Use echo $message; to display the combined string.
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
Step 1: Understand the dot operator usage
In PHP, the dot operator is used to join or concatenate strings.
Step 2: Differentiate from other operators
It does not perform arithmetic or comparison; it only combines strings.
Final Answer:
Joins two strings together -> Option A
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
Step 1: Identify the concatenation operator
PHP uses the dot (.) operator to join strings, so $a . $b is correct.
Step 2: Check other operators
Plus (+) adds numbers, ampersand (&) is bitwise AND, and asterisk (*) is multiplication.
Final Answer:
$a . $b -> Option B
Quick Check:
Dot (.) concatenates strings [OK]
Hint: Use dot (.) to join strings, not plus (+) [OK]