Skip to main content
infervour.com

Back to all posts

How to String Two Variables Together In PHP?

Published on
3 min read
How to String Two Variables Together In PHP? image

Best PHP Books to Buy in October 2025

1 PHP & MySQL: Server-side Web Development

PHP & MySQL: Server-side Web Development

BUY & SAVE
$29.17 $45.00
Save 35%
PHP & MySQL: Server-side Web Development
2 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$36.49 $65.99
Save 45%
Programming PHP: Creating Dynamic Web Pages
3 PHP in easy steps: Updated for PHP 8

PHP in easy steps: Updated for PHP 8

BUY & SAVE
$13.86 $16.99
Save 18%
PHP in easy steps: Updated for PHP 8
4 PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)

PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)

BUY & SAVE
$38.82 $59.95
Save 35%
PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)
5 Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

BUY & SAVE
$61.34 $95.00
Save 35%
Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL
6 PHP Crash Course: The Complete, Modern, Hands-On Guide

PHP Crash Course: The Complete, Modern, Hands-On Guide

BUY & SAVE
$47.23 $69.99
Save 33%
PHP Crash Course: The Complete, Modern, Hands-On Guide
7 PHP 8 Objects, Patterns, and Practice: Volume 1: Mastering OO Enhancements and Design Patterns

PHP 8 Objects, Patterns, and Practice: Volume 1: Mastering OO Enhancements and Design Patterns

BUY & SAVE
$32.49 $44.99
Save 28%
PHP 8 Objects, Patterns, and Practice: Volume 1: Mastering OO Enhancements and Design Patterns
8 PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)

PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)

BUY & SAVE
$3.99
PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)
9 Head First PHP & MySQL: A Brain-Friendly Guide

Head First PHP & MySQL: A Brain-Friendly Guide

BUY & SAVE
$17.43 $54.99
Save 68%
Head First PHP & MySQL: A Brain-Friendly Guide
+
ONE MORE?

In PHP, you can concatenate or string two variables together by using the concatenation operator (dot operator) or the implicit concatenation.

To use the concatenation operator, you simply use a dot (.) between the two variables. Here is an example:

$variable1 = "Hello"; $variable2 = "world";

$result = $variable1 . " " . $variable2; echo $result; // Output: Hello world

In the above example, we first declared two variables $variable1 and $variable2. Then, by using the dot operator, we concatenated the values of these two variables along with a space in between.

Alternatively, PHP offers implicit concatenation, where you simply place the variables next to each other. Here is an example:

$variable1 = "Hello"; $variable2 = "world";

$result = $variable1 . $variable2; echo $result; // Output: Helloworld

In this case, the values of both variables are concatenated together without any space or special characters like in the previous example.

Whether you choose to use the concatenation operator or implicit concatenation depends on your specific requirements and code readability.

What is the purpose of the sprintf function in PHP?

The purpose of the sprintf function in PHP is to format a string by substituting placeholders with corresponding values. It allows you to build a formatted string by replacing placeholders with actual values dynamically. The function takes a format string as the first argument and any number of additional arguments that will be substituted into the format string.

The format string contains placeholders specified by percent sign (%) followed by a character that represents the type of value to be inserted. The additional arguments specify the actual values to be inserted into the format string in the order they appear. The function returns the formatted string.

This function is useful for generating dynamic strings with variable content, such as constructing log messages, generating HTML output, or creating complex text representations of data.

How to combine two strings and remove whitespace in PHP?

In PHP, you can combine two strings and remove whitespace using the str_replace() and implode() functions. Here's an example code:

$string1 = 'Hello'; $string2 = ' World';

// Combine two strings and remove whitespace $combinedString = implode('', array(str_replace(' ', '', $string1), str_replace(' ', '', $string2)));

// Output the combined string echo $combinedString; // Output: HelloWorld

In this example, we first use the str_replace() function to remove any whitespace from both strings. Then, we use the implode() function to combine the two modified strings into one. Finally, we echo the combined string.

How to combine multiple strings using the sprintf function in PHP?

To combine multiple strings using the sprintf function in PHP, you can follow these steps:

  1. Define a format string that contains placeholders for the values you want to combine. The placeholders should be denoted by %s.
  2. Specify the values you want to combine as separate arguments to the sprintf function.
  3. Use the sprintf function with the format string and the specified values to generate the combined string.
  4. Store or use the combined string as required.

Here's an example:

$string1 = "Hello"; $string2 = "World"; $string3 = "!"; $combinedString = sprintf("%s %s%s", $string1, $string2, $string3);

echo $combinedString; // Output: Hello World!

In the example above, the format string "%s %s%s" contains three placeholders for strings. The three strings ($string1, $string2, and $string3) are specified as separate arguments to the sprintf function. The resulting combined string is stored in the $combinedString variable and then outputted.