• home
  • forum
  • my
  • kt
  • download
  • PHP string basics

    Author: 2007-08-11 18:42:03 From:

    Step 1 - PHP string basic


    PHP string tutorial


    String is a type in PHP and it is a series of characters. There are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.
    However I have a good news as well as there is no limitation in string length in PHP (Of course available memory and PHP ini settings can reduce it).

    You can set a string in three different ways. It's up to you what method do you use, all have pros.
    1. The first and most known way is to specify a string in single quotes. Similar to other programing languages you need to use backslash '\' if you want to display a single quote in your text.
      Code:
      1. <?php
      2. $str_1 = 'Demo text';
      3. $str_2 = 'Demo text with single quote(\')';
      4. echo $str_1;
      5. echo $str_2;
      6. ?>
      PHP F1
    2.  The second option to use double quotes. Between double quotes you can use more escape character in your string. Besides this if you put a variable in the string then PHP will interpret it and display the content of the variable.
      Code:
      1. <?php
      2. $str_1 = "Demo text";
      3. $str_2 = "Demo text with double quote(\")\r\n";
      4. $demo = "Internal";
      5. $str_3 = "This is a $demo string";
      6. echo $str_1;
      7. echo $str_2;
      8. echo $str_3;
      9. ?>
      PHP F1

      Here the most important part is the string defined as $str_3 and its output in line 8 will result this:

      Output:
      This is a Internal string


    3. And last you can use heredoc as well. In this case you define your text between heredoc identifiers. In this case it is DEMO. You need to start it with the operator <<< and then the identifier. If you are ready you need to close the heredoc part by adding the identifier again at the beginning of a new line like this:
      Code:
      1. <?php
      2. $str_1 = <<<DEMO
      3. This is a heredoc message
      4.  
      5. Text.
      6. DEMO;
      7.  
      8. echo $str_1;
      9. ?>
      PHP F1

      As you can see the output is in the same formatting - text indent, new lines - as it was defined. You can use variables in case of heredoc as well as in case of double quoted strings.


    The 2. case is the most interesting string definition version so let's see a bit more how to insert a variable inside a string. (You can use variables in case 3. as well but heredoc is not so common.)

    Step 2 - Variable parsing in string


    PHP string tutorial

    Time to time you need to compose a complete text from sub strings and variables. For example if you want to greet your visitor you want to display a text like:

    "Hello Peter, nice to see you again!"

    Of course you can not hard code the user name but you want to use a variable like $userName. If you use single quoted strings then you can display your text like this:

     

    Code:
    1. <?php
    2. $userName = "Peter";
    3.  
    4. echo 'Hello '.$userName.', nice to see you again!';
    5. ?>
    PHP F1

     

    Using double quotes you can make it more simple:

     

    Code:
    1. <?php
    2. $userName = "Peter";
    3.  
    4. echo "Hello $userName, nice to see you again!";
    5. ?>
    PHP F1

     

    The more variable you need to use the bigger is the difference.

    After the basic case let's see some more complex variable parsing. What if you want to display the text:

    "Peters website"

    Using the above examples the $userName variable contains only the string Peter and if you compose your string as before:

    Code:
    1. <?php
    2. $userName = "Peter";
    3.  
    4. echo "$userNames website";
    5. ?>
    PHP F1


    You will get a warning and the result is not what you expected:

    Output:
    Notice: Undefined variable: userNames in Z:\wdocs\test.php on line 4 website


    To solve this problem you need to enclose the variable in curly braces like in the following example: 

    Code:
    1. <?php
    2. $userName = "Peter";
    3.  
    4. echo "${userName}s website";
    5. echo "{$userName}s website";
    6. ?>
    PHP F1

     

    As you can see it is possible to enclose only the variable name without the $ sign or the complete variable.

    You have to use this solution in case of arrays as well in the following way:

    Code:
    1. <?php
    2. $users = array('U1' => "Peter");
    3.  
    4. echo "Hello {$users['U1']}, nice to see you again!";
    5. ?>
    PHP F1



    You can read about string concatenation in the next section.

    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. ?>
    PHP F1


    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. ?>
    PHP F1


    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. ?>
    PHP F1


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

     

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot