PHP String Operators and Concatenation Practice Questions with Solutions

Introductions

PHP string operators help you combine text, build messages, display user information, and create dynamic content for websites. The two main string operators are the concatenation operator (.) and the concatenation assignment operator (.=). In this chapter, you will practice PHP questions using names, company details, product information, and messages to understand how string concatenation works in real-world programs. PHP String Operators and Concatenation Practice Questions with Solutions to help you understand the concepts.

Question 1: Join Two Strings Using the Concatenation Operator (.)

Problem Statement: Write a PHP program to join a first name and a last name using the concatenation operator.

PHP Solution:

<?php
$firstName = "Rahul";
$lastName = "Sharma";

$fullName = $firstName . " " . $lastName;

echo $fullName;
?>

Sample Output:

Rahul Sharma

Explanation: The . operator joins two or more strings. Here, the first name, a space, and the last name are combined into one string and stored in $fullName.

Concepts Covered: String variables, concatenation operator (.), and string combination.

Question 2: Combine Text and Numbers

Problem Statement: Write a PHP program to display a student’s name, age, and marks in a single sentence using concatenation.

PHP Solution:

<?php
$name = "Priya";
$age = 19;
$marks = 92;

echo "Student " . $name . " is " . $age .
     " years old and scored " . $marks . " marks.";
?>

Sample Output:

Student Priya is 19 years old and scored 92 marks.

Explanation: The concatenation operator joins text and variable values into one output string. PHP converts the integer values to their string representations when concatenating them.

Concepts Covered: String concatenation, variables, integers, and output formatting.

Question 3: Create a Welcome Message

Problem Statement: A website needs to display a personalized welcome message. Write a PHP program that combines a fixed message with a user’s name.

PHP Solution:

<?php
$username = "Amit";

$message = "Welcome to VSIT, " . $username . "!";

echo $message;
?>

Sample Output:

Welcome to VSIT, Amit!

Explanation: The program combines the welcome message with the value stored in $username. This technique is useful for personalized greetings on websites and applications.

Concepts Covered: Concatenation, dynamic messages, and variables.

Question 4: Build a Product Description

Problem Statement: A shop stores a product name, brand, and price. Write a PHP program to combine these values into a product description.

PHP Solution:

<?php
$product = "Laptop";
$brand = "Dell";
$price = 55000;

$description = "Product: " . $product .
               ", Brand: " . $brand .
               ", Price: Rs. " . $price;

echo $description;
?>

Sample Output:

Product: Laptop, Brand: Dell, Price: Rs. 55000

Explanation: Each variable is joined with descriptive text to create a readable product description. This is useful when displaying product information dynamically on an e-commerce website.

Concepts Covered: String concatenation, product details, and dynamic content.

Question 5: Add Text to an Existing String Using (.=)

Problem Statement: Write a PHP program that starts with a short message and adds more text using the concatenation assignment operator.

PHP Solution:

<?php
$message = "PHP";

$message .= " is";
$message .= " easy";
$message .= " to learn.";

echo $message;
?>

Sample Output:

PHP is easy to learn.

Explanation: The .= operator appends text to the existing string stored in a variable.

For example, $message .= " is"; is equivalent to $message = $message . " is";.

Concepts Covered: Concatenation assignment (.=), string updates, and variables.

Question 6: Create a Student Result Message

Problem Statement: Write a PHP program that combines a student’s name, subject, and result status into one sentence.

PHP Solution:

<?php
$name = "Neha";
$subject = "Computer Science";
$result = "Pass";

$message = $name . " has " . $result .
           " in " . $subject . ".";

echo $message;
?>

Sample Output:

Neha has Pass in Computer Science.

Explanation: The program joins the student’s name, result status, and subject into a single sentence. The same technique can be used to display student results stored in variables.

Concepts Covered: Concatenation, multiple variables, and result messages.

Question 7: Create an Email Address Using Concatenation

Problem Statement: Write a PHP program to generate an email address by combining a username with a domain name.

PHP Solution:

<?php
$username = "rahul.sharma";
$domain = "example.com";

$email = $username . "@" . $domain;

echo "Email Address: " . $email;
?>

Sample Output:

Email Address: rahul.sharma@example.com

Explanation: The . operator joins the username, the @ symbol, and the domain name. This example demonstrates string construction; a real application should also validate user-provided email addresses.

Concepts Covered: String concatenation, symbols in strings, and building formatted values.

Question 8: Build a Company Contact Message Using .=

Problem Statement: A company wants to create a contact message containing its name, phone number, and email address. Use .= to build the message.

PHP Solution:

<?php
$company = "ABC Technologies";
$phone = "9876543210";
$email = "info@example.com";

$message = "Company: ";
$message .= $company;
$message .= "\nPhone: ";
$message .= $phone;
$message .= "\nEmail: ";
$message .= $email;

echo nl2br($message);
?>

Sample Output:

Company: ABC Technologies

Phone: 9876543210

Email: info@example.com

Explanation: The .= operator adds each new piece of text to the existing message. The \n characters insert newline characters, and nl2br() converts those newlines into HTML line breaks for browser display.

Concepts Covered: .= operator, multiline strings, \n, and nl2br().

Question 9: Calculate a Bill and Display It Using Concatenation

Problem Statement: A customer purchases 3 notebooks at ₹50 each. Write a PHP program to calculate the total and display a complete bill message using concatenation.

PHP Solution:

<?php
$product = "Notebook";
$quantity = 3;
$price = 50;

$total = $quantity * $price;

$message = "Product: " . $product .
           ", Quantity: " . $quantity .
           ", Unit Price: Rs. " . $price .
           ", Total: Rs. " . $total;

echo $message;
?>

Sample Output:

Product: Notebook, Quantity: 3, Unit Price: Rs. 50, Total: Rs. 150

Explanation: The multiplication operator calculates the total cost. The concatenation operator then combines the product details and calculated amount into a single message.

Concepts Covered: String concatenation, arithmetic operations, and dynamic bill generation.

Question 10: Build an HTML Heading Dynamically Using Concatenation

Problem Statement: Write a PHP program that creates an HTML heading and paragraph using concatenation to display a course name and its duration.

PHP Solution:

<?php
$course = "PHP Programming";
$duration = "3 Months";

$html = "<h2>" . $course . "</h2>";
$html .= "<p>Course Duration: " . $duration . "</p>";

echo $html;
?>

Sample Output in Browser:

PHP Programming

Course Duration: 3 Months

Explanation: The program builds an HTML heading and paragraph by combining HTML tags with variable values. When PHP outputs this string in a browser, the browser renders the HTML elements.

Important: If the course name or other values come from user input, escape them with htmlspecialchars() before inserting them into HTML to prevent HTML injection and cross-site scripting risks.

Concepts Covered: Concatenation, .=, HTML generation, and dynamic website content.

Key Takeaways

  • The . operator joins two or more strings into one string.
  • The .= operator appends text to an existing string variable.
  • PHP can concatenate strings with numbers, such as names, ages, prices, and quantities.
  • Spaces and punctuation must be included explicitly when needed.
  • Concatenation is useful for welcome messages, product descriptions, email construction, and bills.
  • The \n escape sequence represents a newline in a string; nl2br() can display those newlines in HTML.
  • PHP can generate HTML dynamically, but user-supplied values should be escaped before being inserted into HTML.

Frequently Asked Questions

1. What is string concatenation in PHP?

String concatenation means joining two or more strings to create a single string.

2. Which operator is used for string concatenation in PHP?

PHP uses the dot (.) operator to join strings.

3. What is the difference between . and .= in PHP?

The . operator combines values into a string expression. The .= operator appends text to a variable and stores the updated string in that variable.

4. Can PHP concatenate strings and numbers?

Yes. PHP converts numbers to their string representations when they are concatenated with strings.

5. How do you add a space between two strings?

Include a space inside a string, as in $fullName = $firstName . " " . $lastName;.

6. Can concatenation be used with variables?

Yes. Variables are commonly concatenated to create dynamic messages and display information.

7. What does .= do in PHP?

It appends a string to the current value of a variable. For example, $message .= " World"; adds World to $message.

8. How can you display a concatenated string on multiple lines in a browser?

You can use HTML line-break tags such as <br> or combine newline characters (\n) with nl2br().

9. Can PHP concatenation generate HTML?

Yes. PHP can combine HTML tags and variable values into a string that a browser renders as HTML.

10. Why is string concatenation important in PHP?

It helps create personalized messages, dynamic web pages, formatted reports, product details, and other content from variable values.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top