Instructions for using string types in PHP _php tutorial

Source: Internet
Author: User
Tags parse error
Note: PHP does not limit the length of the string. The only limit is PHP's available memory in the computer (the value of the Memory_limit variable in the php.ini file)
There are 4 methods for qualifying string ranges:
1, single quotation marks;
2, double quotation marks;
3, prototype document syntax;
4, Nowdoc syntax (PHP5.3.0 start)

1. If the string uses single quotation marks "'" package, the string in the case of single quotation marks "," and backslash "\" symbol, need to be escaped.
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:\*.*? ';

(The string backslash to be validated for single quote wrapping is escaped)

2. If the string is wrapped in double quotes, the character will be escaped:
Escaped characters Sequence meaning
\ linefeed (LF or 0x0A (Ten) in ASCII)
\ r Carriage return (CR or 0x0D (+) in ASCII)
\ t Horizontal tab (HT or 0x09 (9) in ASCII)
\v Vertical tab (VT or 0x0B (one) in ASCII) (since PHP 5.2.5)
\f Form feed (FF or 0x0C () 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 wrapped in double quotation marks "" "or in the form of a prototype document syntax, the variables in the string will be parsed.
1. Simple syntax:
Because the parser will be greedy to match the characters after $, you should use "{" and "}" for the boundary of the table name variable in order not to make anything out.
Copy CodeThe code is as follows:
$beer = ' Heineken ';
echo "$beer ' s taste is great"; Works "'" is a invalid character for variable names
echo "He drank some $beers"; Won ' t work; ' s ' is a valid character for variable names and the variable is "$beer"
echo "He drank some ${beer}s"; Works
echo "He drank some {$beer}s"; Works
?>

Similarly, the subscript of an array and the properties of an object are not resolved.
Copy CodeThe code is as follows:
These examples is specific to using arrays inside of strings.
When outside of a string, always quote array string keys and does not use
{braces}.
Show All Errors
Error_reporting (E_all);
$fruits = Array (' Strawberry ' = ' red ', ' banana ' = ' yellow ');
Works, but note the this works differently outside a string
echo "A banana is $fruits [banana].";
Works
echo "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 braces. This results in a parse error.
echo "A banana is $fruits [' banana '].";
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. Compound Syntax:
Copy CodeThe code is as follows:
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 was wrong for the same reason as $foo [bar] is wrong outside a string.
In other words, it'll still work, but only because PHP first looks for a
Constant named Foo; An error of level e_notice (undefined constant) would be
Thrown.
echo "This is wrong: {$arr [foo][3]}";
Works. When using the multi-dimensional 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 to modify the specified character in the string:
Strings can be accessed using "[]" and "{}". (Note: "{}" is not recommended for access after php5.3.0)
Note: Using a different type (non-integer) type to access the character specified by the string will return null
Warning:
Writing to a out of range offset pads the string with spaces. Non-integer types is 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 a assigned string is used. Assigning empty string assigns NUL byte.

http://www.bkjia.com/PHPjc/322194.html www.bkjia.com true http://www.bkjia.com/PHPjc/322194.html techarticle Note: PHP does not limit the length of the string. The only limitation is that PHP's available memory in the computer (the value of the Memory_limit variable in the php.ini file) limits the range of strings to a method that has ...

  • 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.