![]() |
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 Using Concatenate MethodThe 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.
Output Welcome to GeeksforGeeks Using String Interpolation MethodThe 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.
Output Hello Geeks!! Using str_pad() MethodThe 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.
Output Hello, Welcome to GeeksforGeeks Using the Concatenation Assignment (.=) OperatorThe .= 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.
Output GeeksforGeeks!! Using the String ConversionString 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.
Output Hello GeeksforGeeks Using the sprintf Function or printf FunctionThe 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.
Output Welcome to GeeksforGeeks Using the strcat FunctionThe 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.
Output Hello, GeeksforGeeks !! Using Array JoinThe 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
Output Hello, Welcome to GeeksforGeeks Using implode() FunctionThe 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.
Output Welcome to GeeksforGeeks Using join() FunctionAnother 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.
Output Welcome to GeeksforGeeks |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |