Note: PHP does not limit the length of string. The only limit is the available memory of PHP on the computer (value of the memory_limit variable in the php. ini file)
There are four methods to limit the string range:
1. Single quotes;
2. Double quotation marks;
3. Prototype Document Syntax;
4. nowdoc syntax (starting with PHP5.3.0)
1. If a string is enclosed by single quotation marks ('), escape the single quotation marks ('), ", and backslash.
Copy codeThe Code is as follows:
// Outputs: Arnold once said: "I'll be back"
Echo 'arnold once said: "I \'ll be back "';
// Outputs: You deleted C :\*.*?
Echo 'you deleted C :\\*.*? ';
// Outputs: You deleted C :\*.*?
Echo 'you deleted C :\*.*? ';
(Whether escape is required for the string backslash enclosed in single quotes)
2. If the character string is enclosed in double quotation marks, the character will be escaped:
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)
\ V vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\ F form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\ 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 the string is enclosed in double quotation marks ("") or in the format of the original document syntax, the variables in the string will be parsed.
1. Simple Syntax:
Because the parser greedily matches the characters after $, "{" and "}" should be used to define the boundaries of table name variables.
Copy codeThe Code is as follows:
<? Php
$ Beer = 'heineken ';
Echo "$ beer's taste is great"; // works; "'" is an invalid character for variable names
Echo "He drank some $ beers"; // won't work;'s is a valid character for variable names but the variable is "$ beer"
Echo "He drank some $ {beer} s"; // works
Echo "He drank some {$ beer} s"; // works
?>
Similarly, the subscript of the array and the attributes of the object are not parsed.
Copy codeThe Code is as follows:
<? Php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote array string keys and do not use
// {Braces }.
// Show all errors
Error_reporting (E_ALL );
$ Fruits = array ('strawberry '=> 'red', 'bana' => 'yellow ');
// Works, but note that this works differently outside a string
Echo "A banana is $ fruits [banana].";
// Works
Echo "A banana is {$ fruits ['bana']}.";
// Works, but PHP looks for a constant named banana first, as described below.
Echo "A banana is {$ fruits [banana]}.";
// Won't work, use braces. This results in a parse error.
Echo "A banana is $ fruits ['bana'].";
// Works
Echo "A banana is". $ fruits ['banana ']. ".";
// Works
Echo "This square is $ square-> width meters broad .";
// Won't work. For a solution, see the complex syntax.
Echo "This square is $ square-> width00 centimeters broad .";
?>
2. Composite Syntax:
Copy codeThe Code is as follows:
<? Php
// Show all errors
Error_reporting (E_ALL );
$ Great = 'fantastic ';
// Won't work, outputs: This is {fantastic}
Echo "This is {$ great }";
// Works, outputs: This is fantastic
Echo "This is {$ great }";
Echo "This is $ {great }";
// Works
Echo "This square is {$ square-> width} 00 centimeters broad .";
// Works
Echo "This works: {$ arr [4] [3]}";
// This is wrong for the same reason as $ foo [bar] is wrong outside a string.
// In other words, it will still work, but only because PHP first looks for
// Constant named foo; an error of level E_NOTICE (undefined constant) will be
// Thrown.
Echo "This is wrong: {$ arr [foo] [3]}";
// Works. When using multi-dimen1_arrays, always use braces around arrays
// When inside of strings
Echo "This works: {$ arr ['foo'] [3]}";
// Works.
Echo "This works:". $ arr ['foo'] [3];
Echo "This works too: {$ obj-> values [3]-> name }";
Echo "This is the value of the var named $ name: {$ name }}";
Echo "This is the value of the var named by the return value of getName () :{$ {getName ()}}";
Echo "This is the value of the var named by the return value of \ $ object-> getName (): {$ {$ object-> getName ()}}";
Access, modify the specified character in the string:
Strings can be accessed using "[]" and. (Note: "{}" is not recommended for access later than php5.3.0)
NOTE: If other types (non-integer) are used to access the character specified by the string, NULL is returned.
Warning:
Writing to an out of range offset pads the string with spaces. non-integer types are converted to integer. illegal offset type emits E_NOTICE. negative offset emits E_NOTICE in write but reads empty string. only the first character of an assigned string is used. assigning empty string assigns NUL byte.