PHP string tutorial - String concatenation, manipulation


Home - Tutorials - Basics

A detailed description how to use PHP strings and string manipulation functions.

Tutorial info:


Name:PHP string tutorial
Total steps:3
Category:Basics
Level:Beginner

Bookmark PHP string tutorial



Step 3 - String concatenation, manipulation


PHP string tutorial

 

As you could see on the previous page we used string concatenation in our example codes. Now let's see how we can compose a string in details.

You can concatenate string in PHP using the dot (.) operator. You can do this more ways as you can see in the following example code:

Code:
  1. <?php
  2. // Use only one type of quoting
  3. $str_1 = "This "."is"." a "."test message";
  4.  
  5. // Use mixed quoting
  6. $str_2 = 'This '."is a".' test message';
  7.  
  8. // Or devide it into more lines
  9. $str_3 = "This";
  10. $str_3 .= " is a";
  11. $str_3 .= " test message";
  12.  
  13. $str_4 = "This"
  14. . " is a"
  15. . " test message";
  16. ?>

String modification

Here I only mention the string modification by characters. As you know strings a a series of characters. It means you can use it as a string. So if you have a string variable $name with a value "Peter" then you can access the firs letter (P) as $name[0].

Code:
  1. <?php
  2. $name = 'Peter';
  3.  
  4. for ($i = 0; $i< 5; $i++){
  5. echo "{$name[$i]}<br/>";
  6. }
  7. ?>

And if you can access it you can of course modify the elements one by one like this:
Code:
  1. <?php
  2. $name = 'Peter';
  3.  
  4. $name[0] = 'M';
  5. $name[4] = 'o';
  6. $name[5] = 'r';
  7.  
  8. echo $name
  9. ?>


For more special string manipulation function visit our other specialized tutorials (coming soon)

 





Previous Step of PHP string tutorial


Tags: php string, php string function, string, php



Follow phpf1 on Twitter




F1 Site Family
AJAX F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.1395