Thursday, September 4, 2025
HomeLanguagesHow to append a string in PHP ?

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:

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
// 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

neveropen

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
// 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

neveropen
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS