PHP/String/String Concatenation

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

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

   <source lang="html4strict">

<?php

   $string = "Thank you for buying ";
   $newstring = $string . "my book!";

?>

 </source>
   
  


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

   <source lang="html4strict">

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

 </source>
   
  


Combining a string and a number

   <source lang="html4strict">

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

 </source>
   
  


Concatenating strings together

   <source lang="html4strict">

<?php $my_string = "Hello Max. My name is: "; $newline = "
"; 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"; ?>

 </source>
   
  


Concatenation operator (".")

   <source lang="html4strict">

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

 </source>
   
  


Joining and Disassembling Strings

   <source lang="html4strict">

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

 </source>