String type usage instructions in PHP _php tips

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

1. If the string uses a single quote "'" package, the string should be escaped if the single quote "," and the backslash "\" symbol appears.
Copy Code code as follows:

Outputs:arnold once said: "I ' ll be back"
Echo ' Arnold once said: I\ ' ll be the back ';
Outputs:you deleted c:\*.*?
Echo ' you deleted C:\\*.* ';
Outputs:you deleted c:\*.*?
Echo ' you deleted C:\*.* ';

(need to verify that the single quote wrapped string backslash needs to be escaped)

2. If the string is wrapped in double quotes, the character will be escaped:
Escaped characters Sequence meaning
\ linefeed (LF or 0x0A () 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 regular expression-a character in octal notation
\x[0-9a-fa-f]{1,2} The sequence of characters matching regular expression-a character in hexadecimal notation

If the string is wrapped in double quotes "" "or in the form of a prototype document syntax, the variables in the string are parsed.
1. Simple grammar:
Because the parser will greedy for matching the characters after the $, so, in order to do nothing, you should use "{" and "}" to table the bounds of the name variable.
Copy Code code as follows:

<?php
$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 to 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 an array and the properties of an object are not resolved.
Copy Code code as follows:

<?php
These examples are specific to using arrays inside of strings.
When outside of a string, always quote array string keys and don't use
{braces}.
Show All Errors
Error_reporting (E_all);
$fruits = Array (' Strawberry ' => ' red ', ' banana ' => ' yellow ');
Works, but-this is 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-a-described.
echo "A Banana is {$fruits [banana]}.";
Won ' t work, use braces. This is 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, the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>

2. Compound Grammar:
Copy Code code 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 to the same reason as $foo [bar] is wrong outside a string.
In the other words, it'll still work, but only because PHP-a 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, modify the specified character in the string:
The string can be accessed using the [] and ' {} '. (Note: php5.3.0 is not recommended to use ' {} ' access later)
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 are converted to integer. Illegal offset type emits e_notice. Negative offset emits e_notice in write but reads empty string. Only the "a 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.