Contact Our Development Team
Free Code Tutorials & Open Source Code
More About Strings
Tutorials > PHP > More About Strings
The Concatenation Operator
We've seen in the previous tutorial how strings can be constructed by inserting variable names within a double-quoted string. Strings can also be concatenated (joined) using the concatenation ('.') operator:
<?php
    $a = "Hello";
    $b = "World";
    $c = $a . " " . $b;     //Equivalent to $c = "$a $b";
    echo($c);
?>
This produces the following output:
Hello World
The Concatenating Assignment Operator
It is often necessary to append one string to another. The concatenating assignment operator (.=) is the most efficient way to do this:
<?php
    $a = "Hello ";
    $b = "World";
    
    //The following statments all produce an identical result: ($a = "Hello World")
    $a = "$a$b";    //Variable names are interpreted within a double-quoted string.
    $a = $a . $b;   //Using the concatenation operator.
    $a .= $b;       //Using the concatenating assignment operator.
?>
Manipulating Strings Further
Here, we explore a few of the most useful functions provided by PHP for manipulating strings. A full list of string handling functions is available from the PHP website here.
Changing the case of a string: strtolower() and strtoupper()
We often want to convert a string to upper or lower case to implement case-insensitivity. For example, usernames for websites which require a login are usually case-insensitive - 'joebloggs' and 'JoeBloggs' should refer to the same user. The strtolower() and strtoupper() functions take one argument - a string - and convert that string to lower or upper case respectively:
<?php
    $a = "MyString";
    $b = strtolower($a);
    $c = strtoupper($b);
    
    echo("\$a = \"$a\"<br>");
    echo("\$b = \"$b\"<br>");
    echo("\$c = \"$c\"");
?>
This produces the following output:
$a = "MyString"
$b = "mystring"
$c = "MYSTRING"
Escape Character
The echo statements in the above code introduce another concept - the escape character. PHP's escape character, a backslash (\\), tells the interpreter that the character following it should be interpreted in a different way. The first echo statement, echo("\$a = \"$a\"<br>");, contains two examples. The dollar sign would usually be interpreted as the beginning of a variable. Here, we simply want to echo a literal dollar sign, so we precede it with a backslash. Similarly, the double quotes surrounding the $a would normally denote the end of the string, so we must precede them with a backslash to tell PHP that they should in fact be treated as part of the string itself. Some other useful escape sequences include:
  • \\' - The single quote needs to be escaped only when used inside a string enclosed by single quotes. For example, the strings "Bob's" and 'Bob\\'s' are equivalent.
  • \\r - The 'carriage return' (CR) character.
  • \\n - The 'linefeed' (LF) character, used on POSIX systems to denote a new line. In Windows, the sequence \\r\\n is used to represent a new line.
  • \\\\ - A backslash character
  • \\0 - The NULL character.
Page Responses
Currently there have been no responses to this page...
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
     
Copyright ©2009, Wired IDS Ltd. | Licensed under Creative Commons Attribution Share-Alike | Load time: 0.3105 seconds