Instructions on how to concatenate php string

Admin   PHP   212  2020-08-12 14:09:43

Using strings in PHP and concatenating in PHP is a regular job when you learn to program or making projects with PHP.

String in PHP and how to concatenate strings in PHP

So what is the string used for? The string here can be assigned to a variable of value to combine with one or more other variables to create a data structure of your liking.

The string used in PHP is quite diverse and is used with variables in double quotes "or" and some other special characters like ampersands , ; To ensure accurate data and no errors you need to use \ to replace and avoid "escaped" data.

Here are some examples of strings in PHP

'This is the string in PHP'
'That girl is "I still love"
'1992'
'Today is May 5, 2019'

Using string in PHP, replace a string in PHP correctly

Here are two examples of using and replacing false/correct PHP strings for your reference and note

echo "That girl" I still love ""; // Wrong Use: FATAL ERROR Error message syntax error, unexpected
echo "That girl \" I still love \" "; // Correct Use: That girl "I still love"

How to concatenate strings in PHP as variables

String concatenation in PHP is not difficult as long as you understand the rules for concatenating simple and accurate.

The first thing to note is that you see your string being used with a '' or "" sign to determine how to match the variable correctly and appropriately.

For example: Assigning a value to the variable $date coincides with the date of the calendar today and also the day you get paid ^^, try this small exercise.

$date = "05/05/2019";
$lar = "Date". $date. "is the date your salary was received"; // If the string uses "", you can concatenate or concatenate
$lar = 'Date'. $date. ' is your payday '; // If string uses ampersand, then you need to concatenate
echo $lar;

In the file above the variable $lar is used in "" so to concatenate strings (as a variable) you need to use ". $date." to add the variable value $date to the string.


Code #1:
 

// First String
$a = 'Hello';
 
// Second String
$b = 'World!';
 
// Concatenation Of String
$c = $a.$b;
 
// print Concatenate String
echo " $c \n";

Output: HelloWorld!

Code #2:
 

// First String
$fname = 'John';
 
// Second String
$lname = 'Carter!';
 
// Concatenation Of String
$c = $fname." ".$lname;
 
// print Concatenate String
echo " $c \n";

Output: John Carter!