PHP/String/String Concatenation

Материал из Web эксперт
Перейти к: навигация, поиск

A period (.) character is used to combine two separate variables into a single string

 
<?php
    $string = "Thank you for buying ";
    $newstring = $string . "my book!";
?>



A string and an integer value are added, and the result is an integer value.

 
<?php
$a="5";
$b= 7 + $a;
echo "7 + $a = $b";
?>



Combining a string and a number

 
<?php
$str = "This is an example of ". 3 ." in the middle of a string.";
echo $str;
?>



Concatenating strings together

 
<?php
$my_string = "Hello Max. My name is: ";
$newline = "<br />";
echo $my_string . "Paula" . $newline;
echo "Hi, I"m Max. Who are you? " . $my_string . $newline;
echo "Hi, I"m Max. Who are you? " . $my_string . "Paula";
?>



Concatenation operator (".")

 
<?
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>



Joining and Disassembling Strings

 
<?php 
$string1 = "Hello"; 
$string2 = " World!"; 
$string3 = $string1 . $string2; 
?>