Use of the string type in PHP

Source: Internet
Author: User
Tags parse error

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.CopyCodeThe 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 a string is enclosed in double quotation marks, all characters are 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 code The 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 code The 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.