PHP:
שרשור מחרוזות
How to: (איך לעשות זאת:)
In PHP, you concatenate strings with the dot (.
) operator. Here’s how:
// Basic string concatenation
$greeting = "שלום";
$name = "דוד";
$welcomeMessage = $greeting . ", " . $name . "!";
echo $welcomeMessage; // Outputs: שלום, דוד!
Combining with other variables and functions:
$firstPart = "אני ";
$secondPart = "מתכנת PHP.";
$wholeSentence = $firstPart . $secondPart;
echo $wholeSentence; // Outputs: אני מתכנת PHP.
// Using concatenation with a function
function addExclamation($string) {
return $string . "!";
}
echo addExclamation("נהדר"); // Outputs: נהדר!
Deep Dive (צלילה לעומק)
String concatenation dates back to the earliest programming days. PHP uses the .
operator, unlike JavaScript’s +
or Python’s join()
method. Variables within double quotes are parsed, but single quotes are literal; hence, for complexity’s sake, concatenation is clearer. PHP 8 introduced “Stringable” interface allowing objects to be concatenated directly if they implement a __toString()
method.
See Also (ראה גם)
- The PHP official documentation on strings: php.net/manual/en/language.types.string.php