Web Development Tutorials




  
  

PHP Strings

A string is a series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible. It is no problem for a string to become very large. There is no practical bound to the size of strings imposed by PHP, so there is no reason at all to worry about long strings. String values can be used literally or be assigned to variables.

In PHP, a string literal can be displayed in three ways:

  • single quoted strings
  • double quoted strings
  • heredoc syntax strings
  • newdoc syntax strings

This tutorial demonstrates the use of single quoted and double quoted strings. The heredoc syntax is not covered. Detailed information on this syntax can be found in the PHP Documentation.

Single Quoted Strings

Single quotes provide the easiest method for working with strings. Using this method, strings are surrounded by single quotes (''). If single quotes are needed as part of the display, they must be escaped with the backslash ("\") character. While single quotes provide an easy method for working with strings, single quotes do not support the use of interpolation (see section 3-1 Scalar Variables). The examples below illustrate the use of single quotes.

  <?php 
  
    //A literal string displayed in the browser window
    echo 'PHP was developed in 1994 by Rasmus Lerdorf';

    //A literal string assigned to a variable
    $string = 'Since its development, PHP has become a popular scripting language.';

    echo $string;

    //escaping single quotes
    echo 'The array contains the values \'2,5,3,4\'.';

    //invalid attempt to expand a variable inside of a single quote string
    $name = 'John Smith';
    echo 'The user's name is $name';
 
  ?>
  
RESULT:

   PHP was developed in 1994 by Rasmus Lerdorf
   Since its development, PHP has become a popular scripting language.
   The array contains the values '2,5,3,4'.
   Parse error: syntax error, unexpected 's' (T_STRING), expecting ',' or ';'
   

The first line of output is a result of an echo statement that displays the literal single quoted string in the browser window. The second line of output is displayed after a single quoted string is assigned to a variable. The variable is then displayed using the echo statement. The next line of output demonstrates the use of the escape character. The single quoted string contains a set of single quotes used to contain the array elements. Finally, a parse error is generated when a variable is coded inside of a single quote.

Double Quoted Strings

PHP strings can also be displayed using double quotes (""). If PHP strings are enclosed in double quotes, it is possible to take advantage of interpolation. With double quoted strings, PHP also supports more escape characters. These characters are described in the table below.

Operator	Description
\n	line feed
\r	carriage return
\t	horizontal tab
\\	Backslash
\$	dollar sign
\"	double quote
  <?php 
  
    echo "PHP is supported by many operating systems including Windows and Linux.";

    $name = "John";
    echo "The user's name is $name.";

    $fruits = array('grapes', 'peaches', 'strawberries');
    echo "My favorite fruit is $fruits[0].";
 
  ?>
  
RESULT:

   PHP is supported by many operating systems including Windows and Linux.
   The user's name is John.
   My favorite fruit is grapes.

The first line displays a double quoted string using the echo statement. The second line of output is produced when a string includes a variable. The variable is expanded and its contents are displayed along with the string. Next, an array variable is expanded and displayed along with the string.

String Functions

PHP includes a number of String functions. The following lists some of the most common functions.

  • strlen(string) determines the length of a string.
  • ltrim(string) strips whitespace from the beginning of a string.
  • rtrim(string) strips whitespace from the end of a string.
  • strpbrk(string, char) searches the string for the character (char) and returns false or string beginning with the character found.
  • strtoupper(string) converts string to uppercase.
  • strtolower(string) converts string to lowercase.
  • strrev(string) reverses a string.
  • eregi(string pattern, string subject) performs a case insensitive expression match. Searches subject for a match to the regular expression given in pattern.

The following block of code demonstrates how to use PHP string functions.

  <?php
  
   $string = "Hello World";
   $another_string = "Welcome to PHP";

   echo strlen($string);
   echo strtoupper($another_string);
   echo strrev($another_string);
   echo strpbrk($string, "W");
 
  ?>
RESULT:

   11
   WELCOME TO PHP
   PHP ot emocleW
   World
 

The first line (11) displays the length of the string, "Hello World". Next, the string, "Welcome to PHP", is converted to upper case and displayed in the browser window. This string is also used with the strrev function to reverse the order of the string. Finally, the string, "Hello World", is searched until the "W" character is found. Since the first occurrence of the character occurs with the text "World", this string is displayed.


TOP | NEXT: Date/Time