瀛楃涓诧紝涓涓瓧绗︿覆 灏辨槸鐢变竴绯诲垪鐨勫瓧绗︾粍鎴愶紝鍥犳锛屼竴涓瓧绗﹀氨鏄竴涓瓧鑺傘傝繖灏辨槸璇达紝涓涓瓧鑺傚彧鑳芥湁256绉嶄笉鍚岀殑鍙樺寲锛岃繖涔熸殫绀轰簡PHP鏃 娉曞師鐢熸敮鎸乁nicode 銆 鏇村淇℃伅鍙弬鑰冨嚱鏁 utf8_encode()鍜 utf8_decode()銆
瀛楃涓
涓涓瓧绗︿覆 灏辨槸鐢变竴绯诲垪鐨勫瓧绗︾粍鎴愶紝鍥犳锛屼竴涓瓧绗﹀氨鏄竴涓瓧鑺傘傝繖灏辨槸璇达紝涓涓瓧鑺傚彧鑳芥湁256绉嶄笉鍚岀殑鍙樺寲锛岃繖涔熸殫绀轰簡PHP鏃 娉曞師鐢熸敮鎸乁nicode 銆 鏇村淇℃伅鍙弬鑰冨嚱鏁 utf8_encode()鍜 utf8_decode()銆
Note: 涓涓緢闀跨殑瀛楃涓叉槸娌℃湁闂鐨勶紝PHP瀵逛簬瀛楃涓; 鑰屽瀛楃涓查暱搴︾殑闄愬埗鍙拰杩愯PHP绋嬪簭鐨勮鍙拌 绠楁満鐨勫唴瀛樺ぇ灏忔湁鍏炽
璇硶
涓涓瓧绗︿覆 閫氳繃涓嬮潰鐨4绉嶆柟娉曟潵瀹氫箟锛
鍗曞紩鍙
鍙屽紩鍙
heredoc 璇硶缁撴瀯
nowdoc 璇硶缁撴瀯 (鑷狿HP 5.3.0浠ュ悗)
鍗曞紩鍙
瀹氫箟涓涓瓧绗︿覆 鐨勬渶绠鍗曠殑鏂规硶鏄敤鍗曞紩鍙锋妸瀹冨寘鍥磋捣鏉 (鏍囩偣绗﹀彿 ')銆
濡傛灉鎯宠杈撳嚭涓涓崟寮曞彿锛岄渶鍦ㄥ畠鐨勫墠闈㈠姞涓弽鏂滅嚎 ()銆傚湪鍗曞紩鍙峰墠鎴栧湪瀛楃涓茬殑缁撳熬澶 鎯宠杈撳嚭鍙嶆枩绾匡紝杈撳叆涓ゆ潯 (\)銆傛敞鎰忥紝濡傛灉鍦ㄤ换浣曞叾瀹冪殑瀛楃鍓嶅姞浜嗗弽鏂滅嚎锛屽弽鏂滅嚎灏嗕細琚洿鎺ヨ緭鍑恒
Note: 涓嶅儚鍙屽紩鍙 鍜宧eredoc璇硶缁撴瀯锛 鍦ㄥ崟寮曞彿瀛楃涓蹭腑鐨勫彉閲 鍜岀壒娈婂惈涔夌殑瀛楃灏 涓嶄細 琚浛鎹€
<?php
echo 'this is a simple string';
// 鍙互褰曞叆澶氳
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// 杈撳嚭锛 Arnold once said: "I'll be back"
echo 'Arnold once said: "I'll be back"';
// 杈撳嚭锛 You deleted C:*.*?
echo 'You deleted C:\*.*?';
// 杈撳嚭锛 You deleted C:*.*?
echo 'You deleted C:*.*?';
// 杈撳嚭锛 This will not expand: a newline
echo 'This will not expand: a newline';
// 杈撳嚭锛 Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
鍙屽紩鍙
濡傛灉瀛楃涓叉槸鍖呭洿鍦ㄥ弻寮曞彿(")涓紝 PHP灏嗗涓浜涚壒娈婄殑瀛楃杩涜瑙f瀽锛
Escaped characters
Sequence Meaning
鎹㈣ (LF or 0x0A (10) in ASCII)
鍥炶溅 (CR or 0x0D (13) in ASCII)
姘村钩鏂瑰悜鐨 tab(HT or 0x09 (9) in ASCII)
v 绔栫洿鏂瑰悜鐨 tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
f 鎹㈤〉 (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\ 鍙嶆枩绾
$ 缇庨噾dollar鏍囪
" 鍙屽紩鍙
[0-7]{1,3} 绗﹀悎璇ヨ〃杈惧紡椤哄簭鐨勫瓧绗︿覆鏄竴涓叓杩涘埗鐨勫瓧绗
x[0-9A-Fa-f]{1,2} 绗﹀悎璇ヨ〃杈惧紡椤哄簭鐨勫瓧绗︿覆鏄竴涓崄鍏繘鍒剁殑瀛楃
鍜屽崟寮曞彿 瀛楃涓蹭竴鏍凤紝 濡傛灉杈撳嚭涓婅堪涔嬪鐨勫瓧绗︼紝鍙嶆枩绾夸細琚墦鍗板嚭鏉ャ PHP5.1.1浠 鍓嶏紝{$var} 涓殑鍙嶆枩绾胯繕涓嶄細琚樉绀哄嚭鏉ャ
鐢ㄥ弻寮曞彿瀹氫箟鐨 瀛楃涓叉渶閲嶈鐨勭壒寰佹槸鍙橀噺浼氳杩涜锛屾洿澶氫俊鎭瀛楃涓茶В鏋愩
Heredoc缁撴瀯
绗笁绉嶅畾涔夊瓧绗︿覆鐨勬柟娉曟槸鐢╤eredoc鍙ユ硶缁撴瀯锛<<<銆傚湪璇ユ彁绀 绗﹀悗闈紝瑕佸畾涔変釜鏍囪瘑绗︼紝鐒跺悗鏄竴涓柊琛屻傛帴涓嬫潵鏄瓧绗︿覆 鏈韩锛屾渶鍚庤鐢ㄥ墠闈㈠畾涔夌殑鏍囪瘑绗︿綔涓虹粨鏉熸爣蹇椼
缁撴潫鏃舵墍寮曠敤鐨勬爣璇嗙蹇呴』鍦ㄤ竴琛岀殑寮濮嬩綅缃紝 鑰屼笖锛屾爣璇嗙鐨勫懡鍚嶄篃瑕佸儚鍏跺畠鏍囩涓鏍烽伒瀹圥HP鐨勮鍒欙細鍙兘鍖呭惈 瀛楁瘝銆佹暟瀛楀拰涓嬪垝绾匡紝骞朵笖涓嶈兘鐢ㄦ暟瀛楀拰涓嬪垝绾夸綔涓哄紑澶淬
Warning
瑕佹敞鎰忕殑鏄粨鏉熸爣璇嗙杩欒闄や簡 鍙兘鏈変竴涓垎鍙(;)澶栵紝缁濆涓嶈兘鍖呮嫭 鍏跺畠瀛楃銆傝繖鎰忓懗鐫鏍囪瘑绗︿笉鑳界缉杩涳紝鍒嗗彿鐨勫墠鍚庝篃涓嶈兘鏈変换浣曠┖鐧芥垨tabs銆傛洿閲嶈鐨勬槸缁撴潫鏍囪瘑绗︾殑鍓嶉潰蹇呴』鏄釜琚湰鍦 鎿嶄綔绯荤粺璁ゅ彲鐨勬柊琛屾爣绛撅紝姣斿鍦║NIX鍜孧ac OS X绯荤粺涓槸 锛岃岀粨鏉熸爣璇嗙锛堝彲鑳芥湁涓垎鍙凤級鐨勫悗闈篃蹇呴』璺熶釜 鏂拌鏍囩銆
濡傛灉涓嶉伒瀹堣瑙勫垯瀵艰嚧缁撴潫鏍囩涓嶁滃共鍑鈥濓紝PHP灏嗚涓哄畠涓嶆槸缁撴潫鏍囪瘑绗﹁岀户缁鎵俱傚鏋滃湪鏂囦欢缁撴潫鍓嶄篃娌℃湁鎵惧埌涓涓纭殑缁撴潫鏍囪瘑绗︼紝PHP灏嗕細鍦ㄦ渶鍚庝竴 琛屼骇鐢熶竴涓彞娉曢敊璇
Heredocs缁撴瀯涓嶈兘鐢ㄦ潵鍒濆鍖朿lass锛岃屼粠PHP 5.3浠ュ悗锛屽垯璇ラ檺鍒跺彧鑳界敤鍦ㄥ寘鍚彉閲忕殑鎯呭喌涓嬨
Example #1 闈炴硶鐨勭ず渚
<?php
class foo {
public $bar = <<<EOT
bar
EOT;
}
?>
Heredoc缁撴瀯灏辫薄鏄病鏈変娇鐢ㄥ弻寮曞彿鐨勫弻寮曞彿瀛楃涓诧紝 杩欏氨鏄鍦╤eredoc缁撴瀯涓紩鍙蜂笉鐢ㄨ鏇挎崲锛屼絾鏄笂鏂囦腑鍒楀嚭鐨勫瓧绗 ( 绛)涔熷彲浣跨敤銆 鍙橀噺灏嗚鏇挎崲锛屼絾鍦╤eredoc缁撴瀯涓瓧绗︿覆琛ㄨ揪澶嶆潅鍙橀噺鏃讹紝瑕佹牸澶栧皬 蹇冦
Example #2 Heredoc缁撴瀯鐨勫瓧绗︿覆绀轰緥
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
/* 鍚湁鍙橀噺鐨勬洿澶嶆潅绀轰緥 */
class foo
{
var $foo;
var $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': x41
EOT;
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
My name is "MyName". I am printing some Foo.
Now, I am printing some Bar2.
This should print a capital 'A': A
涔熷彲浠ユ妸Heredoc缁撴瀯鐢ㄥ湪鍑芥暟鍙傛暟涓潵浼犺緭鏁版嵁锛
Example #3 Heredoc缁撴瀯鍦ㄥ弬鏁颁腑鐨勭ず渚
<?php
var_dump(array(<<<EOD
foobar!
EOD
));
?>
鍦≒HP 5.3.0浠ュ悗锛屼篃鍙互鐢℉eredoc缁撴瀯鏉ュ垵濮嬪寲闈欐佸彉閲忓拰绫荤殑灞炴у拰甯搁噺锛
Example #4 浣跨敤Heredoc缁撴瀯鏉ュ垵濮嬪寲闈欐佸
<?php
// 闈欐佸彉閲
function foo()
{
static $bar = <<<LABEL
Nothing in here...
LABEL;
}
// 绫荤殑甯搁噺銆佸睘鎬
class foo
{
const BAR = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}
?>
鍦≒HP 5.3.0涓繕鍦℉eredoc缁撴瀯涓敤鍙屽紩鍙锋潵澹版槑鏍囧織绗︼細
Example #5 鍦╤eredoc缁撴瀯涓娇鐢ㄥ弻寮曞彿
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
Note:
PHP4鎵嶅紩鍏ヤ簡Heredoc缁撴瀯銆
Nowdoc缁撴瀯
灏辫薄heredoc缁撴瀯绫讳技浜庡弻寮曞彿瀛楃涓诧紝Nowdoc缁撴瀯鏄被浼间簬鍗曞紩鍙峰瓧绗︿覆鐨勩侼owdoc缁撴瀯寰堣薄heredoc缁撴瀯锛屼絾鏄 nowdoc涓嶈繘琛岃В鏋愭搷浣 銆 杩欑缁撴瀯寰堥傚悎鐢ㄥ湪涓嶉渶瑕佽繘琛岃浆涔夌殑PHP浠g爜鍜屽叾瀹冨ぇ娈垫枃鏈備笌SGML鐨 <![CDATA[ ]]> 缁撴瀯鏄敤鏉ュ0鏄庡ぇ娈电殑涓嶇敤瑙f瀽鐨勬枃鏈被浼硷紝nowdoc缁撴瀯涔熸湁鐩稿悓鐨勭壒寰併
涓涓猲owdoc缁撴瀯涔熺敤鍜宧eredocs缁撴瀯涓鏍风殑鏍囪 <<<锛 浣嗘槸璺熷湪鍚庨潰鐨勬爣蹇楃瑕佺敤 鍗曞紩鍙锋嫭璧锋潵锛屽氨鍍<<<'EOT'杩欐牱銆俬eredocs缁撴瀯鐨勬墍鏈夎鍒欎篃鍚屾牱閫傜敤浜巒owdoc缁 鏋勶紝灏ゅ叾鏄粨鏉熸爣蹇楃鐨勮鍒欍
Example #6 Nowdoc缁撴瀯瀛楃涓茬ず渚
<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
/* 鍚湁鍙橀噺鐨勬洿澶嶆潅鐨勭ず渚 */
class foo
{
public $foo;
public $bar;
function foo()
{
$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}
$foo = new foo();
$name = 'MyName';
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': x41
EOT;
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': x41
Note:
涓嶈薄 heredocs缁撴瀯锛宯owdocs缁撴瀯鍙互鐢ㄥ湪浠绘剰鐨勯潤鎬佹暟鎹幆澧冧腑锛屾渶鍏稿瀷鐨勭ず渚嬫槸鐢ㄦ潵鍒濆鍖栫被鐨勫睘鎬ф垨甯搁噺锛
Example #7 琛ㄦ佹暟鎹殑绀轰緥
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Note:
Nowdoc缁撴瀯鏄湪PHP 5.3.0涓姞鍏ョ殑銆
鍙橀噺瑙f瀽
褰撳瓧绗︿覆鐢ㄥ弻寮曞彿鎴杊eredoc缁撴瀯瀹氫箟鏃讹紝鍏朵腑鐨勫彉 閲忓皢浼氳瑙f瀽銆
杩欓噷鍏辨湁涓ょ璇硶瑙勫垯锛氫竴绉嶇畝鍗 瑙勫垯锛屼竴绉嶅鏉傝 鍒欍傜畝鍗曠殑鍙ユ硶瑙勫垯鏄渶甯哥敤鍜屾渶鏂逛究鐨勶紝 瀹冨彲浠ョ敤鏈灏戠殑浠g爜鍦ㄤ竴涓瓧绗︿覆涓姞鍏ュ彉閲忥紝 鏁扮粍 鍊硷紝鎴 瀵硅薄灞炴с
澶嶆潅鐨勫彞娉曡鍒欐槸鍦≒HP4浠ュ悗鍔犲叆鐨勶紝琚姳鎷彿鍖呭洿鐨勮〃杈惧紡鏄叾鏄庢樉鏍囪銆
绠鍗曞彞娉曡鍒
褰揚HP瑙f瀽鍣ㄩ亣鍒颁竴涓編鍏冪鍙 ($) 锛 瀹冧細鍜屽叾瀹冨緢澶氳В鏋愬櫒涓鏍凤紝鍘诲敖閲忓舰鎴愪竴涓悎娉曠殑鍙橀噺鍚嶃傚彲浠ョ敤鑺辨嫭 鍙锋潵鏄庣‘鍙橀噺鍚嶇殑鐣岀嚎銆
<?php
$beer = 'Heineken';
echo "$beer's taste is great"; //鏈夋晥锛涘崟寮曞彿"'"鏄潪娉曠殑鍙橀噺鍚嶇粍鎴愬厓绱
echo "He drank some $beers"; //鏃犳晥;瀛楁瘝s鏄湁鏁堢殑鍙橀噺鍚嶇粍鎴愬厓绱狅紝浣嗘槸杩欓噷鐨勫彉閲忔槸$beer
echo "He drank some ${beer}s"; // 鏈夋晥
echo "He drank some {$beer}s"; // 鏈夋晥
?>
绫讳技鐨勶紝涓涓 鏁扮粍 绱㈠紩鎴栦竴涓 瀵硅薄 灞炴т篃鍙瑙f瀽銆傛暟缁勭储寮曡鐢ㄦ柟鎷彿 (]) 鏉ヨ〃绀鸿竟闄咃紝 瀵硅薄灞炴у垯鏄拰涓婅堪鐨勫彉閲忚鍒欑浉鍚屻
<?php
// 涓嬮潰鐨勪緥瀛愭槸鍦ㄥ瓧绗︿覆涓紩鐢ㄦ暟缁
// 褰撴暟缁勫浜庡瓧绗︿覆澶栭儴鏃讹紝瑕佹妸鏁扮粍鐨勫肩敤鎷彿鎷捣鏉ヤ笖涓嶈鐢ㄨ姳鎷彿{ }
// 鏄剧ず鎵鏈夐敊璇
error_reporting(E_ALL);
$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
// 鏈夋晥锛涗絾鏄敞鎰忓湪瀛楃涓插闈笉鑳借繖鏍峰紩鐢ㄦ暟缁
echo "A banana is $fruits[banana].";
// 鏈夋晥
echo "A banana is {$fruits['banana']}.";
// 鏈夋晥锛屼絾鏄疨HP浼氬厛瀵绘壘甯搁噺banana
echo "A banana is {$fruits[banana]}.";
// 鏃犳晥锛岃鐢ㄨ姳鎷彿锛岃繖閲屽皢浼氫骇鐢熶竴涓В鏋愰敊璇
echo "A banana is $fruits['banana'].";
// 鏈夋晥
echo "A banana is " . $fruits['banana'] . ".";
// 鏈夋晥
echo "This square is $square->width meters broad.";
// 鏃犳晥锛岃В鍐虫柟娉曡澶嶆潅缁撴瀯
echo "This square is $square->width00 centimeters broad.";
?>
濡傛灉鎯宠琛ㄨ揪鏇村鏉傜殑缁撴瀯锛岃鐢ㄥ鏉傚彞娉曡鍒欍
澶嶆潅鍙ユ硶瑙勫垯
澶嶆潅鍙ユ硶瑙勫垯涓嶆槸缁撴瀯澶嶆潅鑰屽懡鍚嶏紝鑰屾槸鍥犱负瀹冨彲浠ヤ娇鐢ㄥ鏉傜殑琛ㄨ揪寮忋
浠讳綍鎯崇敤鍦ㄥ瓧绗︿覆涓爣閲忓彉閲忥紝鏁扮粍鍙橀噺鎴栧璞″睘鎬ч兘鍙娇鐢ㄨ繖绉嶆柟娉曘 鍙渶绠鍗曞湴鍍忓湪瀛楃涓蹭互澶栫殑鍦版柟閭f牱鍐欏嚭琛ㄨ揪寮忥紝 鐒跺悗鐢ㄨ姳鎷彿{鍜 }鎶婂畠鎷捣鏉ャ 鐢变簬 { 鏃犳硶琚浆涔夛紝鍙湁 $ 瑕佺揣鎸ㄧ潃 {鎵嶄細琚鍑烘潵锛屽彲浠ョ敤 {$ 鏉ヨ〃杈 {$銆備笅闈㈢殑绀轰緥鍙互鏇村ソ鐨勮В閲婏細
<?php
// 鏄剧ず鎵鏈夐敊璇
error_reporting(E_ALL);
$great = 'fantastic';
// 鏃犳晥锛岃緭鍑:?This?is?{?fantastic}
echo "This is { $great}";
// 鏈夋晥锛岃緭鍑猴細 This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// 鏈夋晥
echo "This square is {$square->width}00 centimeters broad.";
// 鏈夋晥
echo "This works: {$arr[4][3]}";
// 杩欐槸閿欒鐨勮〃杈惧紡锛屽洜涓哄氨璞$foo[bar] 鐨勬牸寮忎笉鑳藉湪瀛楃涓蹭互澶栫殑鍦版柟浣跨敤涓鏍枫
// 鎹㈠彞璇濊锛屽彧鏈夊湪PHP鑳芥壘鍒板父閲廸oo 鐨勫墠鎻愪笅鎵嶄細姝e父宸ヤ綔锛涜繖閲屼細浜х敓涓涓狤_NOTICE?(undefined?constant)绾у埆鐨勯敊璇
echo "This is wrong: {$arr[foo][3]}";
// 鏈夋晥锛屽綋鍦ㄥ瓧绗︿覆涓娇鐢ㄥ閲嶆暟缁勬椂锛屼竴瀹氳鐢ㄦ嫭鍙峰皢瀹冩嫭璧锋潵
echo "This works: {$arr['foo'][3]}";
// 鏈夋晥
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()}}";
// 鏃犳晥锛岃緭鍑猴細 This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";
?>
涔熷彲浠ュ湪瀛楃涓蹭腑鐢ㄥ彉閲忔潵璋冪敤绫荤殑灞炴с
<?php
class foo {
var $bar = 'I am bar.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo "{$foo->$bar} ";
echo "{$foo->$baz[1]} ";
?>
浠ヤ笂渚嬬▼浼氳緭鍑猴細
I am bar.
I am bar.
Note:
鍑芥暟銆佽涓恒佺被鐨勯潤鎬佸彉閲忓拰绫荤殑甯搁噺鍙湁鍦≒HP 5浠ュ悗鎵嶅彲鍦 {$} 涓娇鐢ㄣ傜劧鑰岋紝鍙湁鍦ㄧ敤杩斿洖鐨勫间綔涓哄悕 绉扮殑鍙橀噺瀛樺湪鐨勬儏鍐典笅鎵嶄細杩涜澶勭悊锛屽彧鍗曚竴浣跨敤鑺辨嫭鍙 ({}) 鏃犳硶澶勭悊浠庡嚱鏁版垨琛屼负鐨勮繑鍥炲兼垨浠庣被鐨勫父閲忔垨闈欐佸彉閲忕殑杩 鍥炲笺
<?php
// 鏄剧ず鎵鏈夐敊璇
error_reporting(E_ALL);
class beers {
const softdrink = 'rootbeer';
public static $ale = 'ipa';
}
$rootbeer = 'A & W';
$ipa = 'Alexander Keith's';
// 鏈夋晥锛岃緭鍑猴細 I'd like an A & W
echo "I'd like an {${beers::softdrink}} ";
// 涔熸湁鏁堬紝杈撳嚭锛 I'd like an Alexander Keith's
echo "I'd like an {${beers::$ale}} ";
?>
瀛樺彇鍜屼慨鏀瑰瓧绗︿覆涓殑瀛楃
瀛楃涓蹭腑鐨勫瓧绗﹀彲浠ラ氳繃涓涓互0涓哄紑濮嬬殑锛岀敤绫讳技鏁扮粍缁撴瀯涓殑鏂规嫭鍙峰寘鍚搴旂殑鏁板瓧鏉ユ煡鎵惧拰淇敼锛屾瘮濡 $str[42]锛 鍙互鎶 瀛楃涓叉兂鍍忔暟缁 銆 鍑芥暟 substr() 鍜 substr_replace()鍙互鐢ㄦ潵瀹炵幇澶氫簬涓涓瓧绗 鐨勬儏鍐点
Note: 瀛楃涓蹭负浜嗗悓鏍风殑鐩殑涔熷彲浠ョ敤鑺辨嫭鍙凤紝姣斿 $str{42}锛屼絾鏄紝 鍦 PHP 5.3.0涓笉鎺ㄨ崘浣跨敤杩欑鏍煎紡锛屽簲璇ョ敤鏂规嫭鍙凤紝灏卞儚 $str[42]銆
Warning
鏂规嫭鍙蜂腑鐨勬暟瀛楄秴鍑鸿寖鍥村皢浼氫骇鐢熺┖鐧姐傞潪鏁存暟绫诲瀷琚浆鎹㈡垚鏁存暟锛岄潪鏁存暟绫诲瀷杞彉鎴愭暣鏁帮紝闈炴硶绫诲瀷浼氫骇鐢熶竴涓 E_NOTICE绾у埆閿欒锛岃礋鏁板湪鍐欏叆鏃朵細浜х敓涓涓狤_NOTICE锛屼絾璇 鍙栫殑鏄┖瀛楃涓层傝鎸囧畾鐨勫瓧绗︿覆鍙湁绗竴涓瓧绗﹀彲鐢紝绌哄瓧绗︿覆鐢ㄦ寚瀹氫负绌哄瓧鑺傘備笅闈负鑻辨枃鍘熸枃锛 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.
Example #8 Some string examples
<?php
// 鍙栧緱瀛楃涓茬殑绗竴涓瓧绗
$str = 'This is a test.';
$first = $str[0];
// 鍙栧緱瀛楃涓茬殑绗笁涓瓧绗
$third = $str[2];
// 鍙栧緱瀛楃涓茬殑鏈鍚庝竴涓瓧绗
$str = 'This is still a test.';
$last = $str[strlen($str)-1];
// 淇敼瀛楃涓茬殑鏈鍚庝竴涓瓧绗
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e';
?>
Note:
鐢 [] 鎴 {} 瀛樺彇鍏跺畠绫诲瀷鐨勫彉閲忓彧浼氳繑鍥 NULL.
鏈夌敤鐨勫嚱鏁板拰鎿嶄綔绗
瀛楃涓插彲浠ョ敤'.' (鐐) 鎿嶄綔绗﹁繛鎺ヨ捣鏉ワ紝 娉ㄦ剰 '+' (鍔犲彿) 鎿嶄綔绗 娌℃湁 杩欎釜鍔熻兘銆 鏇村淇℃伅鍙傝 瀛楃涓叉搷浣滅 銆
瀵逛簬瀛楃涓 鐨勬搷浣滄湁寰堝鏈夌敤鐨勫嚱鏁般
鍙互鍙傝 瀛楃涓插嚱鏁 浜嗚В澶ч儴鍒嗗嚱鏁帮紝 楂樼骇鐨勬煡鎵&鏇挎崲鍔熻兘鍙互鍙傝 姝e垯琛ㄨ揪寮忓嚱鏁 鎴 Perl绫诲瀷鐨勬鍒 琛ㄨ揪寮忓嚱鏁般
鍙﹀杩樻湁URL瀛楃涓茬殑鍑芥暟锛 涔熸湁鍔犲瘑/瑙e瘑瀛楃涓茬殑鍑芥暟銆 (mcrypt 鍜 mhash).
鏈鍚庯紝鍙互鍙傝 瀛楃绫诲瀷鍑芥暟銆
杞崲鎴愬瓧绗︿覆
涓涓煎彲浠ラ氳繃鍦ㄥ叾鍓嶉潰鍔犱笂(string)鎴栫敤strval()鍑芥暟鏉ヨ浆鍙樻垚 瀛楃涓层 鍦ㄤ竴涓渶瑕佸瓧绗︿覆鐨勮〃杈惧紡涓紝瀛楃涓蹭細鑷姩杞彉锛屾瘮濡傚湪浣跨敤鍑芥暟 echo() 鎴 print() 鏃讹紝 鎴栧湪涓涓彉閲忓拰涓涓 瀛楃涓 杩涜姣旇緝鏃讹紝灏变細鍙戠敓杩欑杞彉 绫诲瀷 鍜 绫诲瀷杞崲 鍙互鏇村ソ鐨勮В閲婁笅闈㈢殑浜嬫儏锛屼篃鍙弬鑰冨嚱 鏁 settype() 銆
涓涓猙oolean TRUE 鍊艰杞崲鎴 瀛楃涓"1"銆 Boolean FALSE 琚浆鎹㈡垚"" (绌虹殑瀛楃涓)銆 杩欑杞彉鍙互鍦 boolean 鍜 瀛楃涓 涔嬮棿寰杩旇繘琛屻
涓涓 鏁存暟 鎴 娴偣鏁 琚浆鍙樹负鏁板瓧鐨勫瓧闈㈡牱寮忕殑瀛楃涓 (鍖呮嫭 娴偣鏁颁腑鐨勬寚鏁伴儴鍒)锛屼娇鐢ㄦ寚鏁拌鏁版硶鐨勬诞鐐规暟 (4.1E+6)涔熷彲杞彉銆
Note:
鍦ㄨ剼鏈満鎵(category LC_NUMERIC)瀹氫箟浜嗗皬鏁扮偣锛屾洿澶氬彲浠ュ弬鑰冨嚱鏁 setlocale()銆
鏁扮粍杞崲鎴 瀛楃涓 "Array"锛屽洜姝わ紝 echo() 鍜 print() c鏃犳硶鏄剧ず鍑鸿鏁扮粍鐨勫笺傚鏋滄樉绀轰竴涓暟缁勫硷紝鍙互鐢 echo $arr['foo']杩欑缁撴瀯锛屾洿澶氬唴瀹硅涓嬫枃銆
鍦≒HP 4涓璞¤杞崲鎴 瀛楃涓 "Object"锛 鏋滀负浜嗚皟璇曞師鍥犻渶瑕佹墦鍗板嚭瀵硅薄鐨勫硷紝鏂规硶瑙佹鏂囥備负浜嗗緱鍒板璞$殑绫荤殑鍚嶇О锛屽彲浠ョ敤 get_class() 鍑芥暟銆 鍦≒HP5涓紝 鍙互鐢 __toString 銆
璧勬簮鎬讳細琚浆鍙樻垚"Resource id #1"杩欑缁撴瀯鐨 瀛楃涓 锛 鍏朵腑鐨 1 鏄疨HP鍒嗛厤缁欒璧勬簮鐨勭嫭鐗规暟瀛椼備笉鐢ㄨ繃澶氬叧娉ㄨ繖绉嶇粨鏋勶紝瀹冮┈涓婅杞彉浜嗐備负浜嗗緱鍒颁竴涓 resource绫诲瀷锛屽彲浠ョ敤鍑芥暟 get_resource_type()銆
NULL 鎬绘槸琚浆鍙樻垚绌虹殑瀛楃涓层
濡備笂闈㈡墍璇寸殑锛岀洿鎺ユ妸鏁扮粍锛 瀵硅薄鎴 璧勬簮 杞崲鎴 瀛楃涓 涓嶄細寰楀埌瓒呭嚭鍏惰嚜韬殑鏇村淇℃伅銆傚彲浠ヤ娇鐢ㄥ嚱鏁 print_r() 鍜 var_dump() 鍒楀嚭杩欎簺绫诲瀷鐨勫唴瀹广
澶ч儴鍒嗙殑PHP鍊煎彲浠ヨ浆鍙樻垚 瀛楃涓瞫 鏉ラ暱鏈熷偍瀛橈紝杩欒绉颁綔涓茶鍖栵紝鍙互鐢ㄥ嚱鏁 serialize() 鏉ュ疄鐜般 濡傛灉PHP寮曟搸璁惧畾鏀寔 WDDX 锛 PHP鍊间篃鍙偍瀛樻垚XML鏍煎紡銆
瀛楃涓茶浆鍙樻垚鏁板瓧
褰撲竴涓瓧绗︿覆 琚敤鍦ㄤ簡涓涓暟瀛楃殑鐜涓紝缁撴灉鍜岀被鍨嬪涓嬶細
濡傛灉瀛楃涓 娌℃湁鍖呭惈 '.'锛'e'鎴'E' 骞朵笖鏁板瓧鍊肩鍚堟暣鏁扮被鍨嬬殑闄愬畾 ( PHP_INT_MAX瀹氫箟鐨)锛 杩欎釜 瀛楃涓 鍙璁ゅ畾鏄竴涓 integer锛 鍦ㄥ叾瀹冩儏鍐典笅琚瀹氫负涓涓猣loat銆
瀛楃涓茬殑寮濮嬮儴鍒嗙粰瀹氫簡瀹冪殑鍊硷紝濡傛灉 瀛楃涓 浠ュ悎娉曠殑鏁板瓧寮濮嬶紝杩欎釜鏁板瓧鍙洿鎺ヤ娇鐢ㄣ 鍚﹀垯锛屽煎氨 鏄 0 (闆)銆 鍚堟硶鏁板肩敱绗﹀彿锛屽悗闈㈣窡鐫涓涓垨澶氫釜鏁板瓧锛堝彲鑳芥湁涓皬鏁扮偣锛夛紝鍐嶈窡鐫鍙夌殑鎸囨暟绗﹀彿濡'e' 鎴 'E'锛屽悗闈㈣窡鐫涓涓垨澶氫釜鏁板瓧銆
<?php
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
鏇村淇℃伅鍙互鍙傝僓nix鎵嬪唽涓殑strtod(3)銆
鏈妭涓殑绀轰緥鍙互閫氳繃澶嶅埗/绮樿创鍒颁笅闈㈢殑浠g爜涓潵鏄剧ず锛
<?php
echo "$foo==$foo; type is " . gettype ($foo) . "<br /> ";
?>
涓嶈鎯冲儚鍦–璇█涓殑閭f牱锛岄氳繃涓涓暣鏁拌浆鎹㈠緱鍒扮浉搴斿瓧绗︼紝浣跨敤鍑芥暟 ord() 鍜 chr() 瀹炵幇ASCII鐮佸拰瀛楃闂寸殑杞崲銆
浠ヤ笂灏辨槸PHP绫诲瀷涔嬪瓧绗︿覆浠嬬粛鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒