PHP Manual
Prev | Chapter 6. Types | Next

# Strings

Strings can be specified using one of two sets of delimiters.

If the string is enclosed in double-quotes ("), variables within the string will be expanded (subject to some parsing limitations). As in C and Perl, the backslash ("\") character can be used in specifying special characters:

Table 6-1. Escaped characters

sequence | meaning
--- | ---
`\n` | newline
`\r` | carriage
`\t` | horizontal tab
`\\` | backslash
`\$` | dollar sign
`\"` | double-quote
`\[0-7]{1,3}` | the sequence of characters matching the regular expression is a character in octal notation
`\x[0-9A-Fa-f]{1,2}` | the sequence of characters matching the regular expression is a character in hexadecimal notation

You can escape any other character, but a warning will be issued at the highest warning level.

The second way to delimit a string uses the single-quote ("'") character. When a string is enclosed in single quotes, the only escapes that will be understood are "\\" and "\'". This is for convenience, so that you can have single-quotes and backslashes in a single-quoted string. Variables will not be expanded inside a single-quoted string.

Another way to delimit strings is by using here doc syntax ("<<<"). One should provide an identifier after `<<<`, then the string, and then the same identifier to close the quotation. The closing identifier must begin in the first column of the line.

Example 6-1. Here doc string quoting example
      1
      2 $str = <<<EOD
      3 Example of string
      4 spanning multiple lines
      5 using heredoc syntax.
      6 EOD;
      7

> Note: Here doc support was added in PHP 4.

Strings may be concatenated using the '.' (dot) operator. Note that the '+' (addition) operator will not work for this. Please see String operators for more information.

Characters within strings may be accessed by treating the string as a numerically-indexed array of characters, using C-like syntax. See below for examples.

Example 6-2. Some string examples
      1
      2 <?php
      3 /* Assigning a string. */
      4 $str = "This is a string";
      5
      6 /* Appending to it. */
      7 $str = $str . " with some more text";
      8
      9 /* Another way to append, includes an escaped newline. */
     10 $str .= " and a newline at the end.\n";
     11
     12 /* This string will end up being '<p>Number: 9</p>' */
     13 $num = 9;
     14 $str = "<p>Number: $num</p>";
     15
     16 /* This one will be '<p>Number: $num</p>' */
     17 $num = 9;
     18 $str = '<p>Number: $num</p>';
     19
     20 /* Get the first character of a string  */
     21 $str = 'This is a test.';
     22 $first = $str[0];
     23
     24 /* Get the last character of a string. */
     25 $str = 'This is still a test.';
     26 $last = $str[strlen($str)-1];
     27 ?>
     28

## String conversion

When a string is evaluated as a numeric value, the resulting value and type are determined as follows.

The string will evaluate as a double if it contains any of the characters '.', 'e', or 'E'. Otherwise, it will evaluate as an integer.

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

When the first expression is a string, the type of the variable will depend on the second expression.

      1
      2 $foo = 1 + "10.5";              // $foo is double (11.5)
      3 $foo = 1 + "-1.3e3";            // $foo is double (-1299)
      4 $foo = 1 + "bob-1.3e3";         // $foo is integer (1)
      5 $foo = 1 + "bob3";              // $foo is integer (1)
      6 $foo = 1 + "10 Small Pigs";     // $foo is integer (11)
      7 $foo = 1 + "10 Little Piggies"; // $foo is integer (11)
      8 $foo = "10.0 pigs " + 1;        // $foo is integer (11)
      9 $foo = "10.0 pigs " + 1.0;      // $foo is double (11)
     10

For more information on this conversion, see the Unix manual page for strtod(3).

If you would like to test any of the examples in this section, you can cut and paste the examples and insert the following line to see for yourself what's going on:

      1
      2 echo "\$foo==$foo; type is " . gettype( $foo ) . "<br>\n";
      3

Prev | Home | Next
Floating point numbers | Up | Arrays