PHP Concatenate String And Variable
Last Updated : Aug 1, 2023
IN - PHP
In this article we will show you the solution of php concatenate string and variable, the purpose of this tutorial is to teach you how to concatenate strings, variables, and arrays in PHP.
PHP uses the concatenate method to combine strings and variables into a single string; it also joins arrays and variables.
For php developers, concatenation is a very commonly used operator, since it helps them join strings in their everyday programming tasks.
In PHP, concatenating strings can be accomplished using either the concatenation operator (' . ') or the concatenation assignment operator (' .= ').
When left and right arguments are concatenated, the result will be returned by the concatenation operator (". ").
Every subsequent line is added after the previous one when concatenating.Any type of value will automatically be transformed into a string and concatenated with another string.
We will now discuss the idea of how to concatenate string and variable with an example.
Step Guide On PHP Concatenate String And Variable :-
<?php $mystring1 = "Welcome To TC "; $mystring2 = "Welcome to TalkersCode"; $finalString = sprintf("%s %s", $mystring1, $mystring2); echo($finalString); ?>
- This code starts with the PHP tag opened with ? php.
- Assigning the value "Welcome To TC" to $mystring1, the code declares a variable called $mystring1.
- A second variable, $mystring2, is declared and the value "Welcome to TalkersCode" is assigned to it.
- In the code, another variable is declared called $finalString, and the sprintf function is used to concatenate $mystring1 and $mystring2 with a space in between.
- Assigning $finalString to the final result of the sprintf function, the final string is formatted and returned.
- The final step is to use the echo statement to output the value of $finalString, which is "Welcome To TC Welcome To TalkersCode".
As a result, we have successfully learned how to concatenate string and variable with an example.
We discovered how to combine two or more strings together in PHP using array concatenation operators with the aid of example programmes.
I hope this article on php concatenate string and variable helps you and the steps and method mentioned above are easy to follow and implement.