How to append a string in PHP ?
We have given two strings and the task is to append a string str1 with another string str2 in PHP. There is no specific function to append a string in PHP. In order to do this task, we have the this operator in PHP:
Table of Content
Using Concatenation assignment operator (“.=”)
The Concatenation assignment operator is used to append a string str1 with another string str2.
Syntax:
$x .= $y
Example :
<?php
// PHP program to append a string
// function to append a string
function append_string ($str1, $str2) {
// Using Concatenation assignment
// operator (.=)
$str1 .=$str2;
// Returning the result
return $str1;
}
// Given string
$str1 = "Geeks";
$str2 = "for";
$str3 = "Geeks";
// function calling
$str = append_string ($str1, $str2);
$str = append_string ($str, $str3);
// Printing the result
echo $str;
?>
Output
GeeksforGeeks
Using Concatenation Operator(“.”)
The Concatenation operator is used to append a string str1 with another string str2 by concatenation of str1 and str2.
Syntax:
$x . $y
Example :
<?php
// PHP program to append a string
// Function to append a string
function append_string ($str1, $str2){
// Using Concatenation assignment
// operator (.)
$str = $str1 . $str2;
// Returning the result
return $str;
}
// Given string
$str1 = "Geeks";
$str2 = "for";
$str3 = "Geeks";
// Function calling
$str = append_string ($str1, $str2);
$str = append_string ($str, $str3);
// Printing the result
echo $str;
?>
Output
GeeksforGeeks
Using Double Quotes with Variables (Interpolation)
Using double quotes with variables (interpolation) in PHP allows you to embed variables directly within a string. PHP automatically replaces the variable with its value.
Example:
<?php
$greeting = "Hello";
$name = "world";
$combined = "$greeting, $name!"; // Interpolates $greeting and $name into the string
echo $combined; // Outputs: Hello, world!
?>
Output
Hello, world!
Using sprintf() Function
The sprintf() function in PHP can be used to format and append strings by specifying a format string that includes placeholders for variables. This method allows for more complex string manipulations and formatting.
Example:
<?php
function appendStringsUsingSprintf($str1, $str2) {
return sprintf('%s%s', $str1, $str2);
}
// Example usage:
$str1 = "Hello, ";
$str2 = "World!";
$result = appendStringsUsingSprintf($str1, $str2);
echo $result;
?>
Output
Hello, World!
Using implode() Function
The implode() function in PHP can be used to concatenate elements of an array into a single string, effectively appending multiple strings together.
Example:
<?php
$str1 = "Hello";
$str2 = " World!";
$result = implode("", array($str1, $str2));
echo $result; // Output: Hello World!
?>
Output
Hello World!
Using str_replace Function
The str_replace function in PHP can be used to replace all occurrences of a search string with a replacement string. By using a placeholder, we can effectively append one string to another.
In this approach:
- We define a placeholder string (e.g., {append}) that will be replaced.
- We concatenate the first string $str1 with the placeholder.
- We use str_replace to replace the placeholder with the second string $str2.
Example:
<?php
$str1 = "Hello, ";
$str2 = "World!";
$placeholder = "{append}";
$result = str_replace($placeholder, $str2, $str1 . $placeholder);
echo $result;
?>
Output
Hello, World!
Using array_merge and implode Functions
The array_merge function can be used to merge arrays, and by combining it with the implode function, we can concatenate multiple strings into a single string.
Example:
<?php
// Function to append strings using array_merge and implode
function appendStringsUsingArrayMerge($str1, $str2) {
// Merge the strings into an array
$mergedArray = array_merge([$str1], [$str2]);
// Convert the array back to a string
return implode('', $mergedArray);
}
// Given strings
$str1 = "Hello";
$str2 = " World!";
// Function calling
$result = appendStringsUsingArrayMerge($str1, $str2);
// Printing the result
echo $result;
?>
Output
Hello World!