PHP:
Об'єднання рядків
How to: (Як це зробити:)
Here’s the lowdown on joining strings together in PHP:
$greeting = "Привіт";
$name = "Василь";
// Using the dot operator to concatenate
$welcomeMessage = $greeting . ", " . $name . "!";
echo $welcomeMessage; // Outputs: Привіт, Василь!
Wrap your eyes around another way with double quotes:
$food = "борщ";
$phrase = "Я люблю $food";
echo $phrase; // Outputs: Я люблю борщ
Deep Dive (Занурення у глибину)
Back in PHP’s younger days, string concatenation could only be done with the dot operator. Now you’ve also got the possibility of sticking variables right into double-quoted strings, a bit like how we used “борщ” in our example above.
There’s always more than one way to skin a cat, or in this case, to stitch strings together. You could use sprintf()
or implode()
functions, but for basic concatenation, stick to the dot.
Some sharpen their performance by using braces around variables. Can make PHP parse a smidge faster.
echo "Let's eat some {$food} together!";
See Also (Дивись також)
Hungry for more? Take a look at these:
- PHP String Operators in the PHP Manual
- A deeper dive into strings in the PHP String Functions List