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

sequencemeaning
\nlinefeed (LF or 0x0A (10) in ASCII)
\rcarriage return (CR or 0x0D (13) in ASCII)
\thorizontal tab (HT or 0x09 (9) in ASCII)
\\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

If you attempt to escape any other character, both the backslash and the character will be output. In PHP 3, a warning will be issued at the E_NOTICE level when this happens. In PHP 4, no warning is generated.

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. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Here doc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.

Example 6-1. Here doc string quoting example


<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* More complex example, with variables. */
class foo {
    var $foo;
    var $bar;

    function foo() {
        $this->foo = 'Foo';
        $this->bar = array('Bar1', 'Bar2', 'Bar3');
    }
}

$foo = new foo();
$name = 'MyName';

echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
     

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


<?php
/* Assigning a string. */
$str = "This is a string";

/* Appending to it. */
$str = $str . " with some more text";

/* Another way to append, includes an escaped newline. */
$str .= " and a newline at the end.\n";

/* This string will end up being '<p>Number: 9</p>' */
$num = 9;
$str = "<p>Number: $num</p>";

/* This one will be '<p>Number: $num</p>' */
$num = 9;
$str = '<p>Number: $num</p>';

/* Get the first character of a string  */
$str = 'This is a test.';
$first = $str[0];

/* Get the last character of a string. */
$str = 'This is still a test.';
$last = $str[strlen($str)-1];
?>	  
     

String parsing

When a string is specified in double quotes, variables are parsed within it.

There are two types of syntax, a simple one and a complex one. The simple syntax is the most common and convenient, it provides a way to parse a variable, an array-value, or a object-property.

The complex syntax was introduced in PHP 4, and can by recognised by the curly braces surrounding the expression.

Simple syntax

If a $ is encoutered, the parser will greedily take as much tokens as possible to form a valid variable name. Enclose the the variable name in curly braces if you want to explicitely specify the end of the name.


 $beer = 'Heineken';
 echo "$beer's taste is great"; // works, "'" is an invalid character for varnames
 echo "He drunk some $beers"; // won't work, 's' is a valid character for varnames
 echo "He drunk some ${beer}s"; // works
       

Similary, you can also have an array-index and an object-property parsed. With array-indices, the ']' marks the end of the index, for object-properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables.


 $fruits = array( 'strawberry' => 'red' , 'banana' => 'yellow' );
 echo "A banana is $fruits[banana].";
 echo "This square is $square->width meters broad.";
 echo "This square is $square->width00 centimeters broad."; // won't work,
    // for a solution, see the complex syntax.
 
 
 
       

For anything more complex, you should use the complex syntax.

Complex (curly) syntax

I didn't call this complex because the syntax is complex, but because you can include complex expressions this way.

In fact, you can include any value that is in the namespace in strings with this syntax. You simply write the expression the same way as you would outside the string, and then include it in { and }. Since you can't escape '{', this syntax will only be recognised when the $ is immediately following the {. (Use "{\$" to get a literal "{$"). Some examples to make it clear:


 $great = 'fantastic';
 echo "This is { $great}"; // won't work, outputs: This is { fantastic}
 echo "This is {$great}";  // works, outputs: This is fantastic
 echo "This square is {$square->width}00 centimeters broad."; 
 echo "This works: {$arr[4][3]}";     
 echo "This is wrong: {$arr[foo][3]}"; // for the same reason 
    // as $foo[bar] is wrong outside a string. 
 
 echo "You should do it this way: {$arr['foo'][3]}";
 echo "You can even write {$obj->values[3]->name}";
 echo "This is the value of the var named $name: {${$name}}";
 
 
 // this works, but i disencourage its use, since this is NOT 
 // involving functions, rather than mere variables, arrays and objects.
 $beer = 'Heineken';
 echo "I'd like to have another {${ strrev('reeb') }}, hips";
 
 
       

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.


$foo = 1 + "10.5";              // $foo is double (11.5)
$foo = 1 + "-1.3e3";            // $foo is double (-1299)
$foo = 1 + "bob-1.3e3";         // $foo is integer (1)
$foo = 1 + "bob3";              // $foo is integer (1)
$foo = 1 + "10 Small Pigs";     // $foo is integer (11)
$foo = 1 + "10 Little Piggies"; // $foo is integer (11)
$foo = "10.0 pigs " + 1;        // $foo is integer (11)
$foo = "10.0 pigs " + 1.0;      // $foo is double (11)     
     

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:


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