Home  >  Article  >  Backend Development  >  Instructions for using string type in PHP_PHP Tutorial

Instructions for using string type in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:04745browse

Note: PHP has no limit on the length of strings. The only limitation 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 limit the string range:
1. Single quotation mark;
2. Double Quotation marks;
3. Prototype document syntax;
4. nowdoc syntax (starting with PHP5.3.0)

1. If the string is wrapped with single quotation marks "'", if a single quotation mark appears in the string "," and backslash "" symbols need to be escaped.

Copy code The 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:*.*?';

(It remains to be verified whether the backslashes in the string wrapped in single quotes need to be converted Meaning)

2. If the string is wrapped in double quotes, all characters 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 wrapped in double quotes """ or in the form of raw document syntax, the variables in the string will be parsed.
1. Simple syntax:
Because the parser will greedily match the characters after $, so in order to avoid anything unexpected, "{" and "}" should be used to indicate the boundaries of variable names.
Copy code The code is as follows:

$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, array subscripts and object properties will not be resolved.
Copy code The code is as follows:

// 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', 'banana' => 'yellow');
// Works, but note that 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 code The 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 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 a
// 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-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()}}";


访问,修改字符串中的指定字符:
字符串可以使用"[]"和"{}"进行访问。(注意:php5.3.0以后不建议使用“{}”访问)
注意:使用其他类型(非integer)类型访问字符串指定的字符,都会返回NULL
警告:
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。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322194.htmlTechArticle注意:PHP没有对string的长度做限制。唯一限制的就是PHP在计算机中的可用内存(php.ini文件中的memory_limit变量的值) 限定字符串范围的方法有...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn