From 3de536403cc3a7c5ddc5b5b430fcdc8a9e3a3e75 Mon Sep 17 00:00:00 2001 From: Jeroen van Wolffelaar Date: Wed, 23 May 2001 22:13:58 +0000 Subject: [PATCH] updated the strings section a bit. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@48306 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/types.xml | 548 ++++++++++++++++++++++++++++----------------- 1 file changed, 346 insertions(+), 202 deletions(-) diff --git a/language/types.xml b/language/types.xml index 593a444842..af935620b0 100644 --- a/language/types.xml +++ b/language/types.xml @@ -478,107 +478,216 @@ EXPONENT_DNUM (({LNUM}|{DNUM})[eE][+-]?{LNUM}) 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: - - 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 - - - - -
+ 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. +
+ + + 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. + - - 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. - + + + single quoted + + + + + double quoted + + + + + heredoc syntax + + - - 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. - + + + + 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. + + + In PHP 3, a warning will + be issued at the E_NOTICE level when this + happens. + + + + + Unlike the two other syntaxes, variables will not + be expanded when they occur in single quoted strings. + + + + +echo 'this is a simple string'; +echo 'You can also have embedded newlines in strings, +like this way.'; +echo 'Arnold once said: "I\'ll be back"'; +// output: ... "I'll be back" +echo 'Are you sure you want to delete C:\\*.*?'; +// output: ... delete C:\*.*? +echo 'Are you sure you want to delete C:\*.*?'; +// output: ... delete C:\*.*? +echo 'I am trying to include at this point: \n a newline'; +// output: ... this point: \n a newline + + + + + + Double quoted + + If the string is enclosed in double-quotes ("), + PHP understands more escape sequences for special + characters: + + + 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 + backspace 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 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. + + + + + It is very important to note that the line with the closing + identifier contains no other characters, except + possibly a ;. + That means especially that the identifier + may not be indented, and there + may not be any spaces or tabs after or before the ;. + + + Probably the nastiest gotcha is that there may also + not be a carriage return (\r) at the end of + the line, only + a form feed, a.k.a. newline (\n). + Since Microsoft Windows uses the sequence + \r\n as a line + terminator, your heredoc may not work if you write your + script in a windows editor. However, most programming + editors provide a way to save your files with UNIX + line terminator. + + + - - 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. - - Here doc string quoting example - + + 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. + + Here doc string quoting example + <?php $str = <<<EOD Example of string @@ -606,68 +715,22 @@ Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; ?> - - - - - - - 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. - - - - 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 - + + + + + + + Here doc support was added in PHP 4. + + + + + + Variable parsing - When a string is specified in double quotes, variables are + When a string is specified in double quotes or with + heredoc, variables are parsed within it. @@ -685,10 +748,10 @@ $last = $str[strlen($str)-1]; and can by recognised by the curly braces surrounding the expression. - + Simple syntax - If a $ is encoutered, the parser will + 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 @@ -696,10 +759,10 @@ $last = $str[strlen($str)-1]; - $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 +$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 @@ -720,29 +783,29 @@ $last = $str[strlen($str)-1]; - $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. - - - +$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, + This isn't called complex because the syntax is complex, but because you can include complex expressions this way. @@ -756,29 +819,110 @@ $last = $str[strlen($str)-1]; - $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"; - - +$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}}"; + - -
+ + + + + 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. + + + + For backwards compatibility, you can still use the array-braces. + However, this syntax is deprecated as of PHP 4. + + + + + 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 @@ -832,7 +976,7 @@ echo "\$foo==$foo; type is " . gettype ($foo) . "<br>\n"; -
+ Arrays