How to add a Character to String in PHP ?
Last Updated :
16 Jul, 2024
Given two strings, the task is to add a Character to a String in PHP. The character can be added to a string in many ways in this article we will be using eight methods including the Concatenate Method, the String Interpolation Method, the str_pad() Method, Concatenation Assignment (.=) Operator, String Conversion, The sprintf Function or printf Function, The strcat Function, and Using Array Join.
For Example, Consider the below example where there are two strings:
Input
string1 = "Welcome to" ,
string2 = "GeeksforGeeks"
Output
Welcome to GeeksforGeeks
Using Concatenate Method
The concatenation operator (.) of PHP offers to concatenate strings. Here we will be using the (.) sign to add a Character to the String. The example uses two individual strings to add and make a new string.
Example: The below method illustrates how to add a Character to a String in PHP with the help of the concatenate method.
PHP
<?php
$oldstring = "Welcome to";
$newstring = " GeeksforGeeks";
// Concatenate the characters to the string
$resultstring = $oldstring . $newstring;
// Displaying the output
echo $resultstring;
?>
OutputWelcome to GeeksforGeeks
Using String Interpolation Method
The method is available in PHP 7.0 and later versions. The variables can be directly embedded in the resulting string without using the concatenation operator. With the help of the string interpolation method, we can add a Character to a String.
Example: The below method illustrates how to add a Character to a String in PHP with the help of the string interpolation method.
PHP
<?php
$oldstring = "Hello";
$newstring = " Geeks!! ";
// Using String Interpolation Method
$resultstring = "$oldstring$newstring";
// Displaying the result
echo $resultstring;
?>
Using str_pad() Method
The str-pad() method is used to add a character to either the beginning, the end, or both sides of the string. The string str_pad method takes four parameters including an old string, length, a new string that would be added pad_string, and pad type for defining side (start, end, or both).
Example: The below method illustrates how to add a Character to a String in PHP with the help of the str_pad() method.
PHP
<?php
$oldstr = "Hello, Welcome to";
echo str_pad($oldstr, 31, " GeeksforGeeks", STR_PAD_RIGHT);
?>
OutputHello, Welcome to GeeksforGeeks
Using the Concatenation Assignment (.=) Operator
The .= operator in PHP is the concatenation assignment operator. It is used to concatenate the right operand to the left operand and assign the result to the left operand.
Example: The below method illustrates how to add a Character to a String in PHP with the help of the Concatenation Assignment (.=) Operator.
PHP
<?php
$oldstr = "GeeksforGeeks";
$oldstr .= "!!";
echo $oldstr;
?>
Using the String Conversion
String conversion involves using the concatenation operator (.) to join two strings. The example shows $newstr = $oldstr. " GeeksforGeeks"; concatenates the original string $oldstr with the additional string " GeeksforGeeks".
Example: The below method illustrates how to add a Character to a String in PHP with the help of String Conversion.
PHP
<?php
$oldstr = "Hello";
$newstr = $oldstr . " GeeksforGeeks";
echo $newstr;
?>
OutputHello GeeksforGeeks
Using the sprintf Function or printf Function
The sprintf function returns the formatted string, while printf directly outputs the formatted string. The sprintf and printf functions are used for formatted string output in PHP.
Example: The below method illustrates how to add a Character to a String in PHP with the help of sprintf Function or printf Function.
PHP
<?php
$oldstr = "Welcome to";
$newstr = sprintf("%s GeeksforGeeks", $oldstr);
echo $newstr;
?>
OutputWelcome to GeeksforGeeks
Using the strcat Function
The strcat is not a native function in PHP, the equivalent operation can be achieved using the concatenation operator (.). The example shows $newstr = $oldstr. " GeeksforGeeks"; defines the concatenation of two strings.
Example: The below method illustrates how to add a Character to a String in PHP with the help of strcat Function.
PHP
<?php
$oldstr = "Hello,";
$newstr = $oldstr . " GeeksforGeeks !!";
echo $newstr;
?>
OutputHello, GeeksforGeeks !!
Using Array Join
The implode function is used to join array elements into a string. The example shows $newstr = implode("", $parts); joins the array $parts into a single string with an empty string as the glue between elements.
Example: The below method illustrates how to add a Character to a String in PHP with the help of Array Join
PHP
<?php
$oldstr = "Hello, Welcome to";
$p = [$oldstr, " GeeksforGeeks"];
$newstr = implode("", $p);
echo $newstr;
?>
OutputHello, Welcome to GeeksforGeeks
Using implode() Function
The implode() function in PHP joins array elements into a string, optionally inserting a specified separator between each element.
Example: This method leverages arrays to manage and concatenate strings, offering flexibility in how strings are manipulated and joined together in PHP.
PHP
<?php
// Initial strings
$string1 = "Welcome to";
$string2 = " GeeksforGeeks";
// Create an array of strings
$parts = [$string1, $string2];
// Join array elements into a single string
$newString = implode(' ', $parts);
echo $newString;
?>
OutputWelcome to GeeksforGeeks
Using join() Function
Another method to add a character to a string in PHP involves using the join() function, which is an alias of implode(). This function allows you to join array elements into a string, with a specified separator if needed. This approach can be particularly useful when you have a string split into an array and you want to add a character or string between the elements.
Example: The following example demonstrates how to use the join() function to add a character to a string in PHP.
PHP
<?php
$string1 = "Welcome to";
$string2 = "GeeksforGeeks";
$parts = str_split($string1);
$parts[] = " " . $string2;
$newString = join("", $parts);
echo $newString;
?>
OutputWelcome to GeeksforGeeks
Similar Reads
How to Convert Array to String in PHP?
We are given an array and the task is to convert the array elements into a string. Below are the approaches to convert an array to a string in PHP: Table of Content Using implode() functionUsing json_encode() FunctionUsing sprintfUsing serialize() FunctionUsing implode() functionThe implode() method
2 min read
How to get the last n characters of a PHP string?
Write a PHP program to get last n characters of a given string. Examples: Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. E
1 min read
How to Convert String to Camelcase in PHP?
Given a String containing spaces, the task is to Convert String to Camelcase in PHP. Converting a string to CamelCase is a common operation in PHP, especially when working with variable names or class names. CamelCase is a naming convention where the first letter of each word in a compound word is c
3 min read
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 (".=")Using Concatenation O
4 min read
How to Convert Byte Array to String in PHP?
Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP: Table of Content What is
3 min read
How to trim all strings in an array in PHP ?
Given a string array with white spaces, the task is to remove all white spaces from every object of an array.Examples: Input: arr = ["Geeks ", " for", " Geeks "] Output: Geeks for Geeks Method 1: Using trim() and array_walk() function: trim() Function: The trim() function is an inbuilt function whic
3 min read
How to Iterate Over Characters of a String in PHP ?
This article will show you how to iterate over characters of string in PHP. It means how to loop through an array of characters in a string. There are two methods to iterate over the character of a string, these are: Table of Content Using str_split() function and foreach LoopUsing for LoopUsing mb_
3 min read
How to find number of characters in a string in PHP ?
We have given a string and the task is to count number of characters in a string str in PHP. In order to do this task, we have the following methods in PHP: Table of Content Method 1: Using strlen() MethodMethod 2: Using mb_strlen() MethodMethod 3: Using iconv_strlen() MethodMethod 4: Using grapheme
3 min read
How to Extract Substring from a String in PHP?
Given a String, the task is to extract a substring from a string in PHP. Extracting substrings from a string is a common task in PHP, whether you need to extract a portion of text based on a specific position or find a substring based on a pattern. In this article, we will explore various approaches
3 min read
How to Copy String in PHP ?
Copying a string is a basic operation that all the programming languages offer. In this article, we will explore different approaches to copying a string in PHP, including using the assignment operator, the strval() function, the substr() function and by implementing the strcpy() function. Table of
3 min read