|
|
 |
Strings
A string is series of characters. In PHP,
a character is the same as a byte, that is, there are exactly
256 different characters possible. This also implies that PHP
has no native support of Unicode.
Poznámka:
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.
Syntax
A string literal can be specified in three different
ways.
Single quoted
The easiest way to specify a simple string is to
enclose it in single quotes (the character ').
To specify a literal single
quote, you will need to escape it with a backslash
(\), like in many other languages.
If a backslash needs to occur before a single quote or at
the end of the string, you need to double it.
Note that if you try to escape any
other character, the backslash too will be printed! So
usually there is no need to escape the backslash itself.
Poznámka:
In PHP 3, a warning will
be issued at the E_NOTICE level when this
happens.
Poznámka:
Unlike the two other syntaxes, variables will not
be expanded when they occur in single quoted strings.
Double quoted
If the string is enclosed in double-quotes ("),
PHP understands more escape sequences for special
characters:
Tabulka 7-1. Escaped characters | sequence | meaning |
|---|
| \n | linefeed (LF or 0x0A (10) in ASCII) | | \r | carriage return (CR or 0x0D (13) in ASCII) | | \t | horizontal 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
|
Again, if you try to escape any other character, the
backslash will be printed too!
But the most important pre of double-quoted strings
is the fact that variable names will be expanded.
See string
parsing for details.
Heredoc
Another way to delimit strings is by using heredoc 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.
| Varování |
It is very important to note that the line with the closing
identifier contains no other characters, except
possibly a semicolon (;).
That means especially that the identifier
may not be indented, and there
may not be any spaces or tabs after or before the semicolon.
|
Heredoc 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.
Příklad 7-2. Heredoc 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;
?> |
|
Poznámka:
Heredoc support was added in PHP 4.
Variable parsing
When a string is specified in double quotes or with
heredoc, 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 an object property.
The complex syntax was introduced in PHP 4,
and can be recognised
by the curly braces surrounding the expression.
Simple syntax
If a dollar sign ($) is encountered, the
parser will greedily take as much tokens as possible to form a
valid variable name. Enclose the variable name in curly
braces if you want to explicitly specify the end of the name.
Similarly, you can also have an array index or an object
property parsed. With array indices, the closing square bracket
(]) 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.
For anything more complex, you should use the complex syntax.
Complex (curly) syntax
This isn't called 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 "{\$" or "\{$" to get a literal "{$").
Some examples to make it clear:
String access by character
Characters within strings may be accessed by specifying the
zero-based offset of the desired character after the string
in curly braces.
Poznámka:
For backwards compatibility, you can still use the array-braces.
However, this syntax is deprecated as of PHP 4.
Příklad 7-3. 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};
?> |
|
Useful functions
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.
There are a lot of useful functions for string modification.
See the string functions section
for general functions, the regular expression functions for
advanced find&replacing (in two tastes:
Perl and
POSIX extended).
There are also functions for URL-strings,
and functions to encrypt/decrypt strings
(mcrypt and
mhash).
Finally, if you still didn't find what you're looking for,
see also the character type functions.
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 float 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.
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:
User Contributed Notes Strings |
 |
| There are no user contributed notes for this page. |
| |