Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.
Hello Friends Today, through this tutorial, I will tell you How do you concatenate strings using PHP, PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 With Example. In PHP 8.1 and PHP 8.2, you can concatenate strings using the `.` (dot) operator or the `concat()` function. Here’s how you can use both methods:
Using the dot (.) operator:
<?php // Strings to concatenate $string1 = "Hello"; $string2 = "World"; // Concatenate strings using the dot (.) operator $result = $string1 . ", " . $string2 . "!"; // Output the result echo $result; ?>
Using the `concat()` function:
<?php // Strings to concatenate $string1 = "Hello"; $string2 = "World"; // Concatenate strings using the concat() function $result = concat($string1, ", ", $string2, "!"); // Output the result echo $result; ?>
Both code snippets produce the same output:
Hello, World!
In the first example, the `.` (dot) operator is used to concatenate the strings `$string1`, `”, “`, and `$string2`, along with an exclamation mark. The result is stored in the variable `$result`.
In the second example, the `concat()` function is used, which concatenates all its arguments. Note that `concat()` is a language construct rather than a regular function in PHP. It can accept an arbitrary number of arguments, each of which must be a string.
Both methods are commonly used for string concatenation in PHP. Choose the one that fits your coding style and requirements.