PHP 5.6.0beta1 released

Opérateurs de chaînes

Il y a deux opérateurs de chaînes de caractères string. Le premier est l'opérateur de concaténation ('.'), qui retourne la concaténation de ses deux arguments. Le second est l'opérateur d'affectation concaténant (.=). Reportez-vous à opérateurs d'affectation pour plus de détails.

Exemple #1 Opérateur de concaténation

<?php
$a 
"Bonjour ";
$b $a "Monde !"// $b contient "Bonjour Monde !"

$a "Bonjour ";
$a $a "Monde !"// $a contient "Bonjour Monde !"
?>

Voir aussi les sections du manuel sur les types de chaînes de caractères et les chaînes de caractères.

add a note add a note

User Contributed Notes 10 notes

up
21
hexidecimalgadget at hotmail dot com
5 years ago
If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.

<?php

echo "thr"."ee";           //prints the string "three"
echo "twe" . "lve";        //prints the string "twelve"
echo 1 . 2;                //prints the string "12"
echo 1.2;                  //prints the number 1.2
echo 1+2;                  //prints the number 3

?>
up
22
K.Alex
1 year ago
As for me, curly braces serve good substitution for concatenation, and they are quicker to type and code looks cleaner. Remember to use double quotes (" ") as their content is parced by php, because in single quotes (' ') you'll get litaral name of variable provided:

<?php

 $a
= '12345';

// This works:
 
echo "qwe{$a}rty"; // qwe12345rty, using braces
 
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
 
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
 
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined

?>
up
13
Stephen Clay
8 years ago
<?php
"{$str1}{$str2}{$str3}"; // one concat = fast
 
$str1. $str2. $str3;   // two concats = slow
?>
Use double quotes to concat more than two strings instead of multiple '.' operators.  PHP is forced to re-concatenate with every '.' operator.
up
13
anders dot benke at telia dot com
9 years ago
A word of caution - the dot operator has the same precedence as + and -, which can yield unexpected results.

Example:

<php
$var = 3;

echo "Result: " . $var + 3;
?>

The above will print out "3" instead of "Result: 6", since first the string "Result3" is created and this is then added to 3 yielding 3, non-empty non-numeric strings being converted to 0.

To print "Result: 6", use parantheses to alter precedence:

<php
$var = 3;

echo "Result: " . ($var + 3);
?>
up
7
mariusads::at::helpedia.com
5 years ago
Be careful so that you don't type "." instead of ";" at the end of a line.

It took me more than 30 minutes to debug a long script because of something like this:

<?
echo 'a'.
$c = 'x';
echo 'b';
echo 'c';
?>

The output is "axbc", because of the dot on the first line.
up
0
patel405nishant at gmail dot com
3 days ago
Please note that PHP does not differentiate between double quotes and single quotes when it comes to concatenating.

Meaning that you can use this:

<?php

$var
= "hello";
$world = "world";

echo
"$var" . '$world'; //outputs hello$world

echo "$var" . "$world"; //outputs helloworld

echo "$var" $world; //outputs helloworld

?>

so you can use both at the same time, you can use it to evaluate a portion of the string and keep other parts unevaluated.
up
-6
mostafa dot ali106 at gmail dot com
3 months ago
Please note that PHP does not differentiate between double quotes and single quotes when it comes to concatenating.

Meaning that you can use this:

<?php

$var
= "hello";

echo
"$var" . '$world'; //outputs hello$world

?>

so you can use both at the same time, you can use it to evaluate a portion of the string and keep other parts unevaluated.
up
-9
mostafa dot ali106 at gmail dot com
3 months ago
Please note that PHP does not differentiate between double quotes and single quotes when it comes to concatenating.

Meaning that you can use this:

<?php

$var
= "hello";

echo
"$var" . '$world'; //outputs hello$world

?>

so you can use both at the same time, you can use it to evaluate a portion of the string and keep other parts unevaluated.
up
-12
mostafa dot ali106 at gmail dot com
3 months ago
Please note that PHP does not differentiate between double quotes and single quotes when it comes to concatenating.

Meaning that you can use this:

<?php

$var
= "hello";

echo
"$var" . '$world'; //outputs hello$world

?>

so you can use both at the same time, you can use it to evaluate a portion of the string and keep other parts unevaluated.
up
-17
mostafa dot ali106 at gmail dot com
3 months ago
Please note that PHP does not differentiate between double quotes and single quotes when it comes to concatenating.

Meaning that you can use this:

<?php

$var
= "hello";

echo
"$var" . '$world'; //outputs hello$world

?>

so you can use both at the same time, you can use it to evaluate a portion of the string and keep other parts unevaluated.
To Top