鍩虹锛孭HP 涓殑鍙橀噺鐢ㄤ竴涓編鍏冪鍙峰悗闈㈣窡鍙橀噺鍚嶆潵琛ㄧず銆傚彉閲忓悕鏄尯鍒嗗ぇ灏忓啓鐨勩
PHP 涓殑鍙橀噺鐢ㄤ竴涓編鍏冪鍙峰悗闈㈣窡鍙橀噺鍚嶆潵琛ㄧず銆傚彉閲忓悕鏄尯鍒嗗ぇ灏忓啓鐨勩
鍙橀噺鍚嶄笌 PHP 涓叾瀹冪殑鏍囩涓鏍烽伒寰浉鍚岀殑瑙勫垯銆備竴涓湁鏁堢殑鍙橀噺鍚嶇敱瀛楁瘝鎴栬呬笅鍒掔嚎寮澶达紝鍚庨潰璺熶笂浠绘剰鏁伴噺鐨勫瓧姣嶏紝鏁板瓧锛屾垨鑰呬笅鍒掔嚎銆傛寜鐓ф甯哥殑姝e垯琛ㄨ揪寮忥紝瀹冨皢琚〃杩颁负锛'[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'銆
Note: 鍦ㄦ鎵璇寸殑瀛楁瘝鏄 a-z锛孉-Z锛屼互鍙 ASCII 瀛楃浠 127 鍒 255锛0x7f-0xff锛夈
Note: $this 鏄竴涓壒娈婄殑鍙橀噺锛屽畠涓嶈兘琚祴鍊笺
Tip
璇峰弬瑙乁serland Naming Guide銆
鏈夊叧鍙橀噺鐨勫嚱鏁颁俊鎭鍙橀噺鍑芥暟銆
<?php
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // 杈撳嚭 "Bob, Joe"
$4site = 'not yet'; // 闈炴硶鍙橀噺鍚嶏紱浠ユ暟瀛楀紑澶
$_4site = 'not yet'; // 鍚堟硶鍙橀噺鍚嶏紱浠ヤ笅鍒掔嚎寮澶
$i绔欑偣is = 'mansikka'; // 鍚堟硶鍙橀噺鍚嶏紱鍙互鐢ㄤ腑鏂
?>
鍙橀噺榛樿鎬绘槸浼犲艰祴鍊笺傞偅涔熷氨鏄锛屽綋灏嗕竴涓〃杈惧紡鐨勫艰祴浜堜竴涓彉閲忔椂锛屾暣涓師濮嬭〃杈惧紡鐨勫艰璧嬪煎埌鐩爣鍙橀噺銆傝繖鎰忓懗鐫锛屼緥濡傦紝褰撲竴涓彉閲忕殑鍊艰祴浜堝彟澶栦竴涓彉閲忔椂锛屾敼鍙樺叾涓竴涓彉閲忕殑鍊硷紝灏嗕笉浼氬奖鍝嶅埌鍙﹀涓涓彉閲忋傛湁鍏宠繖绉嶇被鍨嬬殑璧嬪兼搷浣滐紝璇峰弬闃呰〃杈惧紡涓绔犮
PHP 涔熸彁渚涗簡鍙﹀涓绉嶆柟寮忕粰鍙橀噺璧嬪硷細寮曠敤璧嬪笺傝繖鎰忓懗鐫鏂扮殑鍙橀噺绠鍗曠殑寮曠敤锛堟崲瑷涔嬶紝鈥滄垚涓哄叾鍒悕鈥 鎴栬 鈥滄寚鍚戔濓級浜嗗師濮嬪彉閲忋傛敼鍔ㄦ柊鐨勫彉閲忓皢褰卞搷鍒板師濮嬪彉閲忥紝鍙嶄箣浜︾劧銆
浣跨敤寮曠敤璧嬪硷紝绠鍗曞湴灏嗕竴涓 & 绗﹀彿鍔犲埌灏嗚璧嬪肩殑鍙橀噺鍓嶏紙婧愬彉閲忥級銆備緥濡傦紝涓嬪垪浠g爜鐗囨柇灏嗚緭鍑衡淢y name is Bob鈥濅袱娆★細 <?php
$foo = 'Bob'; // 灏 'Bob' 璧嬬粰 $foo
$bar = &$foo; // 閫氳繃 $bar 寮曠敤 $foo
$bar = "My name is $bar"; // 淇敼 $bar 鍙橀噺
echo $bar;
echo $foo; // $foo 鐨勫间篃琚慨鏀
?>
鏈変竴鐐归噸瑕佷簨椤瑰繀椤绘寚鍑猴紝閭e氨鏄彧鏈夋湁鍚嶅瓧鐨勫彉閲忔墠鍙互寮曠敤璧嬪笺 <?php
$foo = 25;
$bar = &$foo; // 鍚堟硶鐨勮祴鍊
$bar = &(24 * 7); // 闈炴硶; 寮曠敤娌℃湁鍚嶅瓧鐨勮〃杈惧紡
function test()
{
return 25;
}
$bar = &test(); // 闈炴硶
?>
铏界劧鍦 PHP 涓苟涓嶉渶瑕佸垵濮嬪寲鍙橀噺锛屼絾瀵瑰彉閲忚繘琛屽垵濮嬪寲鏄釜濂戒範鎯傛湭鍒濆鍖栫殑鍙橀噺鍏锋湁鍏剁被鍨嬬殑榛樿鍊 - 甯冨皵绫诲瀷鐨勫彉閲忛粯璁ゅ兼槸 FALSE锛屾暣褰㈠拰娴偣鍨嬪彉閲忛粯璁ゅ兼槸闆讹紝瀛楃涓插瀷鍙橀噺榛樿鍊兼槸绌哄瓧绗︿覆鎴栬呮暟缁勫彉閲忕殑榛樿鍊兼槸绌烘暟缁勩
Example #1 鏈垵濮嬪寲鍙橀噺鐨勯粯璁ゅ
<?php
// Unset AND unreferenced (no use context) variable; outputs NULL
var_dump($unset_var);
// Boolean usage; outputs 'false' (See ternary operators for more on this syntax)
echo($unset_bool ? "true " : "false ");
// String usage; outputs 'string(3) "abc"'
$unset_str .= 'abc';
var_dump($unset_str);
// Integer usage; outputs 'int(25)'
$unset_int += 25; // 0 + 25 => 25
var_dump($unset_int);
// Float/double usage; outputs 'float(1.25)'
$unset_float += 1.25;
var_dump($unset_float);
// Array usage; outputs array(1) { [3]=> string(3) "def" }
$unset_arr[3] = "def"; // array() + array(3 => "def") => array(3 => "def")
var_dump($unset_arr);
// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" }
$unset_obj->foo = 'bar';
var_dump($unset_obj);
?>
渚濊禆鏈垵濮嬪寲鍙橀噺鐨勯粯璁ゅ煎湪鏌愪簺鎯呭喌涓嬩細鏈夐棶棰橈紝渚嬪鎶婁竴涓枃浠跺寘鍚埌鍙︿竴涓箣涓椂纰颁笂鐩稿悓鐨勫彉閲忓悕銆傚彟澶栨妸 register_globals 鎵撳紑鏄竴涓富瑕佺殑瀹夊叏闅愭偅銆備娇鐢ㄦ湭鍒濆鍖栫殑鍙橀噺浼氬彂鍑篍_NOTICE閿欒锛屼絾鏄湪鍚戜竴涓湭鍒濆鍖栫殑鏁扮粍闄勫姞鍗曞厓鏃朵笉浼氥俰sset() 璇█缁撴瀯鍙互鐢ㄦ潵妫娴嬩竴涓彉閲忔槸鍚﹀凡琚垵濮嬪寲銆
浠ヤ笂灏辨槸PHP鍙橀噺涓殑鍩虹浠嬬粛鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒