Π‘Ρ‚Ρ€ΠΎΠΊΠΎΠ²Ρ‹Π΅ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹

Π’ PHP со строками (string) Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‚ Π΄Π²Π° ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π°. ΠŸΠ΅Ρ€Π²Ρ‹ΠΉ β€” ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΠΈ (Β«.Β»), ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ строку, которая прСдставляСт соСдинСниС Π»Π΅Π²ΠΎΠ³ΠΎ ΠΈ ΠΏΡ€Π°Π²ΠΎΠ³ΠΎ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π°. Π’Ρ‚ΠΎΡ€ΠΎΠΉ β€” ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ присваивания с ΠΊΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΠ΅ΠΉ (Β«.=Β»), ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ присоСдиняСт ΠΏΡ€Π°Π²Ρ‹ΠΉ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ ΠΊ Π»Π΅Π²ΠΎΠΌΡƒ. ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅ ΠΎΠ± этом рассказываСт Ρ€Π°Π·Π΄Π΅Π» Β«ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ присваивания».

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠšΠΎΠ½ΠΊΠ°Ρ‚Π΅Π½Π°Ρ†ΠΈΡ строк

<?php

$a
= "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ";
$b = $a . "ΠœΠΈΡ€!"; // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $b Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ содСрТит строку "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠœΠΈΡ€!"
var_dump($b);

$a = "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ";
$a .= "ΠœΠΈΡ€!"; // ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ $a Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ содСрТит строку "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠœΠΈΡ€!"
var_dump($a);

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
273
K.Alex ΒΆ
13 years 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
162
anders dot benke at telia dot com ΒΆ
22 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
112
Stephen Clay ΒΆ
20 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
91
hexidecimalgadget at hotmail dot com ΒΆ
17 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
11
biziclop ΒΆ
3 years ago
Some bitwise operators (the and, or, xor and not operators: & | ^ ~ ) also work with strings too since PHP4, so you don't have to loop through strings and do chr(ord($s[i])) like things.

See the documentation of the bitwise operators: https://www.php.net/operators.bitwise

<?php var_dump(
  ('23456787654' ^ 'zVXYYhoXDYP'), // 'Hello_World'
  ('(!($)^!)@$@' | '@ddhfIvn2H$'), // 'hello_world'
  ('{}~|o!Wo{|}' & 'Lgmno|Wovmf'), // 'Hello World'
  (~'<0-14)(98'  &   '}}}}}}}}}')  // 'AMPLITUDE'
); ?>

Live demo: https://3v4l.org/MnFeb
up
42
mariusads::at::helpedia.com ΒΆ
17 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.