Lokang 

PHP and MySQL

Concatenating Strings

In PHP, you can concatenate strings using the dot (.) operator. This operator combines two or more strings into a single string. Here are some examples to illustrate string concatenation in PHP:

Basic Concatenation

<?php
$string1 = "Hello";
$string2 = "World!";
$combinedString = $string1 . " " . $string2;
echo $combinedString; // Outputs: Hello World!
?>

Concatenating Multiple Strings

<?php
$part1 = "This ";
$part2 = "is ";
$part3 = "a ";
$part4 = "test.";
$combinedString = $part1 . $part2 . $part3 . $part4;
echo $combinedString; // Outputs: This is a test.
?>

Using Concatenation in Variables

<?php
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs: John Doe
?>

Concatenation with Assignment Operator

PHP provides a convenient concatenation assignment operator (.=) to append a string to an existing variable.

<?php
$string = "Hello";
$string .= ", ";
$string .= "World!";
echo $string; // Outputs: Hello, World!
?>

Concatenation in Function Calls

You can also use concatenation directly in function calls.

<?php
function greet($name) {
   return "Hello, " . $name . "!";
}
echo greet("Alice"); // Outputs: Hello, Alice!
?>

Combining Strings and Variables

<?php
$greeting = "Hello";
$name = "John";
$combinedString = $greeting . ", " . $name . "!";
echo $combinedString; // Outputs: Hello, John!
?>

Example with HTML

String concatenation is often used in generating dynamic HTML content.

<?php
$title = "Welcome";
$content = "This is a dynamic webpage.";
$html = "<html><head><title>" . $title . "</title></head><body>";
$html .= "<h1>" . $title . "</h1>";
$html .= "<p>" . $content . "</p>";
$html .= "</body></html>";
echo $html;
// Outputs the complete HTML document with the dynamic content
?>

These examples show various ways to concatenate strings in PHP, demonstrating the flexibility and power of string manipulation in the language.