String
String is a series of characters. In PHP, the characters and bytes are the same, that is, there are 256 possible possibilities of different characters. This also suggests that PHP has no local support to Unicode. See Functions UTF8_ENCODE () and UTF8_DECode () for information on Unicode support.
Note: A string has no problem, and PHP does not implement the size of the string, so there is no reason to worry about the long string.
grammar
Strings can be defined in three forms.
Single number double quotation mark
apostrophe
The easiest way to specify a simple string is to be enclosed in single quotes (character ').
To represent a single quotes, you need to use a backslash (/) escape, as many other languages. If there is a backslash in the end of the single quotes or the end of the string, it is necessary to use two antilactors. Note If you try to escape any other characters, the reverse slope itself will be displayed! So usually do not need to escape the reverse slope itself.
Note: In PHP 3, a warning of an E_NOTICE level will be issued without this situation.
Note: Unlike other two syntax, variables and escape sequences that appear in single quotes are not replaced by variables.
?
Double quotes
If you have a double quotation mark ("), PHP knows more escape sequences for more special characters:
Table 11-1. Semicone Character
Sequence enrollment / n wrap (LF or ASCII Character 0x0a (10)) / R Enter (Cr or ASCII Character 0x0D (13)) / T Horizontal Table (HT or ASCII Character 0x09 (9)) // Antilasque / $ USD / "Double Quotes / [0-7] {1,3} This regular expression sequence matches a character / x [0-9a-fa-f] {1, 2} represented by an octal symbol Expression sequence matches a character represented by hexadecimal symbols
In addition, if you try to escape any other characters, the reverse slope itself will be displayed!
The most important point of the double quotation string is that the variable name is replaced by a variable value. Details see string parsing.
Delimiter
Another way to deliver a string boundary using a delimiter syntax ("<<<"). A identifier should be provided after <<<, then the string, then the same identifier ends the string.
The end identifier must start from the first column from the line. Similarly, the identifier must also follow the naming rules of any other label in PHP: can only contain alphanumeric underscores, and must begin with the scribe or non-digital characters. Warning is important to point out that the row of end identifiers cannot contain any other characters, except for a semicolon (;). This particularly means that the identifier cannot be indented, and no spaces or tabs cannot be included before and after the semicolon. It is also important to realize that the first character before the end identifier must be a newline character defined in your operating system. For example, in the Macintosh system is / R. If this rule is destroyed so that the end identifier does not "clean", it will not be considered end identifier, and PHP will continue to find. If the appropriate end identifier is not found in this case, it will cause a syntax error that appears on the last line of the script.
The dedicated text is the same as the double quotation string, but there is no double quotation. This means that there is no need to escape quotation in the delimiter text, but it can still be used in the above-mentioned escape code. The variable will be deployed, but it should also be noted when complex variables are expressed in the delimiter text.
Example 11-2. Deriter character string example
PHP $ STR = <<< eodexample of stringspanning multiple limited; / * 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 <<< Eotmy Name IS "$ name". I am Printing Some $ foo-> foo.now, i am printing some {$ foo-> bar [1]}. This SHOULD PRINT A Capital 'A': / x41eot;?>
Note: The delimiter support is added to PHP 4.
Variable analysis
When a string is specified with a double quotation or a delimiter, the variables are parsed.
There are two grammar, a simple and one complicated. The simple syntax is most common and convenient, it provides a method of analyzing variables, array values, or object properties.
The complex syntax is introduced by PHP 4, and an expression can be enclosed in the curneth.
Simple syntax
If you encounter your dollar symbol ($), the parser will take the following characters as much as possible to form a legal variable name. If you want to express the end of the specified name, create a variable with the curneth.
php $ beer = 'Heineken'; echo "$ beer's taste is great"; // Works, "'" is an invalid character for varnamesecho "He Drank Some $ beers"; // Won't work,' s' Is a valid character for varnamesecho "He Drank Some $ {beer} s"; // Worksecho "He Drank Some {$ beer} S"; // Works?>
You can also analyze the array index or object properties. For array indexes, right brackets (]) marks the end of the index. Object properties and simple variables apply the same rule, although there is no small trick like variables for object properties. Php // These examples are specific to using arrays inside of strings.// When outside of a string, always quote your array string keys // and do not use {braces} when outside of strings either.// Let's show all errorserror_reporting (E_ALL); $ fruits = array ( 'strawberry' => 'red', 'banana' => 'yellow'); // Works but note that this works differently outside string-quotesecho "A banana is $ fruits [banana ]. "; // Worksecho" a banana is {$ fruits ['banana']}. "; // Works But PHP Looks for a constant named banana first // as described Below.echo" a banana is {$ fruits [ Banana]}. "// Won't work, use baraces. this results in a parse error.echo" a banana is $ fruits ['banana']. "// Worksecho" a banana is ". $ fruits [ 'Banana']. "; // Worksecho" Square is $ Square-> width meters Broad. "; // Won't work. for a solution, see the complex syntax.echo" this Square is $ Square- > width00 centimeters Broad. ";>>>
For any more complex case, complex syntax should be used.
Complex (curly bracket) grammar
Not because the grammar is complicated, it is complicated, but because this method can contain complex expressions.
In fact, use this syntax you can contain any value in the namespace in the string. Use only to write an expression in the same way outside the string, then contain it in {and}. Because you can't escape "{", this syntax is only recognized when $ keep back ("{/ $" or "{$" is recognized ("{$")). Use some examples to be clearer:
php // let's show all errorsrror_reporting (e_all); $ Great = 'fantastic'; // No, output is: this is {fantastic} echo "this is {$ great}"; // Yes, output: THIS IS fantasticecho "this is {$ great}"; echo "this is $ {great}"; // worksecho "Square is {$ Square-> width} 00 centimeters Broad."; // Worksecho "this works: {$ Arr [4] [3]} "; // this is Wrong for the Same Reason as $ foo [bar] is wrong // outside a string. in Otherwords, IT Will Still Work But // Because PHP First Looks for a constant Named foo, it will // throw an error of level e_notice (undefined constant). Echo "this is Wroge: {$ Arr [foo] [3]}"; // Works. When Using Multi-Dimensional Arrays, Always Use / / Braces Around Arrays When Inside of Stringsecho "this works: {$ arr ['foo'] [3]}"; // Works.echo "this works:" $ arr ['foo'] [3]; echo " You can Even Write {$ OBJ-> VALUES [3] -> Name} "; echo" this is the value of the var named $ name: {$ {$ name}} ";?> Access characters in the string
The characters in the string can be accessed by specifying the offset from zero from zero by specifying the offset of the zero by the currency after the string.
Note: In order to be compatible, you can still use square brackets. However, this syntax is not approved in PHP 4.
Example 11-3. Some string examples
php // Get The First Character of a string $ str = 'this is a test.'; $ first = $ str {0}; // get the third character of a string $ third = $ str {2}; // Get the last character of a string. $ Str = 'this is stock a test.'; $ Last = $ str {strlen ($ str) -1};?>>
Practical functions and operators
The string can be connected with the "." (Click) operator. Note that the " " (plus) operator cannot be used here. See string operators for more information.
There are many utility functions to change strings.
The normal function see the string function library section, advanced search, and replace the regular expression function (two flavors: Perl and POSIX extensions).
There is also a URL string function, as well as functions (Mcrypt and Mhash) of the encryption / decryption string.
Finally, if you still can't find the function you want, see the character type function library.
String conversion
You can convert a value to a string with a (String) tag or STRVAL () function. When a character is a string, the conversion of the string will be automatically completed within the expression range. For example, when you use an ECHO () or Print () function, or when a variable value is compared to a string. Some of the types and type tricks in the reading manual help more clearly. See settype (). The Boolean value TRUE will be converted into a string "1", and the value FALSE will be represented as "" (ie, the string). This allows you to compare between Boolean and strings.
When an integer or floating point number is converted into a string, the string is indicated by these numeric numbers (the floating point number also contains an exponential portion).
The array will be converted into a string "array", so you cannot output the contents of the array through the ECHO () or Print () function. Please refer to the below for more prompts.
Objects will be converted into string "Object". If you need to print an object's member variable, read the following. If you want to get the name of the class that is attached to the object, use the function get_class ().
The resource type will be converted into a string in the format of "Resource ID # 1", where 1 is the unique identifier specified by PHP at runtime. If you want to get the type of resource, use the function get_resource_type ().
NULL will be converted into an empty string.
As shown above, print arrays, objects, or resources, do not give you any useful information about these values itself. See Functions Print_R () and var_dump (), for debugging, these are better printed methods.
You can convert PHP values to strings to permanently store them. This method is called serialization, you can use the function serialize () to complete the operation. If you have established WDDX support when installing PHP, you can also serve the value of PHP to an XML structure.
String conversion to values
When a string is evaluated as a number, the type and value of the result is determined according to the following rules.
If ".", "E" or "e" is included, the string is used as a FLOAT to evaluate. Otherwise it is used as an integer.
This value is determined by the front part of the string. If the string starts with legal digital data, use this number as its value, otherwise its value is 0 (zero). The legal digital data begins with an optional positive and negative number, followed by one or more numbers (optionally including a decimal score), followed by an optional index. Index is a "e" or "e" followed by one or more numbers.
php $ foo = 1 "10.5"; // $ is float (11.5) $ foo = 1 "-1.3e3"; // $ foo is float (-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 = 4 "10.2 Little Piggies"; // $ foo is float (14.2) $ foo = "10.0 pigs" 1; // $ foo is float (11) $ foo = "10.0 PIGS" 1.0; // $ foo is float (11)?> More information for this Conversion See the part of the Unix manual for the STRTOD (3).
If you want to test any example in this section, you can copy and paste these examples and add the following line you will see what will happen:
phpecho "/ $ foo == $ foo; type is". Gettype ($ foo). "
/ n";?>>>
Do not expect to get the encoding of the character when converting a character into an integer (you may also do this in C). If you want to convert between character codes and characters, use the ORD () and CHR () functions.
Add a Note
User contributed notes
String
Shadda At Gmail Dot COM
25-Jan-2005 08:39
IT's Fun To Use Heredoc Syntax In Conjunction with Sprintf, and Quite Effective In My Opinion.sprintf (<<< EOSthe Dog Ran Over the% s, and Landed on The% SEOS, 'CAT', 'FLOOR'); Dunno if anyone .
Lelon At Lelon Dot Net
28-OCT-2004 03:01
You can use the complex syntax to put the value of both object properties AND object methods inside a string For example ...
10-Sep-2004 06:47
Re: Jonathan Lozinski's note on vim syntax highlighting, we use EOHTML, EOSQL, and EOJAVASCRIPT quite frequently for HTML, SQL, and JavaScript, respectively.eg: Will BE Highlight Correctly for SQL Under Vim.
Jonathan Lozinski
07-aug-2004 03:03
A note on the heredoc stuff.If you're editing with VI / VIM and possible other syntax highlighting editors, then using certain words is the way forward. If you use <<< HTML for example, then the text will be hightlighted for HTML !! i Just Found this Out and used sed to alter all eof to html.javascript also Works, and Possibly Others. The only Thing About <<< JavaScript is that you can't add the