绫诲瀷杞崲鐨勫垽鍒
PHP 鍦ㄥ彉閲忓畾涔変腑涓嶉渶瑕侊紙鎴栦笉鏀寔锛夋槑纭殑绫诲瀷瀹氫箟锛涘彉閲忕被鍨嬫槸鏍规嵁浣跨敤璇ュ彉閲忕殑涓婁笅鏂囨墍鍐冲畾鐨勩備篃灏辨槸璇达紝濡傛灉鎶婁竴涓瓧绗︿覆鍊艰祴缁欏彉閲 var锛寁ar 灏辨垚浜嗕竴涓瓧绗︿覆銆傚鏋滃張鎶婁竴涓暣鍨嬪艰祴缁 var锛岄偅瀹冨氨鎴愪簡涓涓暣鏁般
绫诲瀷杞崲鐨勫垽鍒
PHP 鍦ㄥ彉閲忓畾涔変腑涓嶉渶瑕侊紙鎴栦笉鏀寔锛夋槑纭殑绫诲瀷瀹氫箟锛涘彉閲忕被鍨嬫槸鏍规嵁浣跨敤璇ュ彉閲忕殑涓婁笅鏂囨墍鍐冲畾鐨勩備篃灏辨槸璇达紝濡傛灉鎶婁竴涓瓧绗︿覆鍊艰祴缁欏彉閲 var锛寁ar 灏辨垚浜嗕竴涓瓧绗︿覆銆傚鏋滃張鎶婁竴涓暣鍨嬪艰祴缁 var锛岄偅瀹冨氨鎴愪簡涓涓暣鏁般
PHP 鐨勮嚜鍔ㄧ被鍨嬭浆鎹㈢殑涓涓緥瀛愭槸鍔犲彿鈥+鈥濄傚鏋滀换浣曚竴涓搷浣滄暟鏄诞鐐规暟锛屽垯鎵鏈夌殑鎿嶄綔鏁伴兘琚綋鎴愭诞鐐规暟锛岀粨鏋滀篃鏄诞鐐规暟銆傚惁鍒欐搷浣滄暟浼氳瑙i噴涓烘暣鏁帮紝缁撴灉涔熸槸鏁存暟銆傛敞鎰忚繖骞舵病鏈夋敼鍙樿繖浜涙搷浣滄暟鏈韩鐨勭被鍨嬶紱鏀瑰彉鐨勪粎鏄繖浜涙搷浣滄暟濡備綍琚眰鍊间互鍙婅〃杈惧紡鏈韩鐨勭被鍨嬨
<?php
$foo = "0"; // $foo 鏄瓧绗︿覆 (ASCII 48)
$foo += 2; // $foo 鐜板湪鏄竴涓暣鏁 (2)
$foo = $foo + 1.3; // $foo 鐜板湪鏄竴涓诞鐐规暟 (3.3)
$foo = 5 + "10 Little Piggies"; // $foo 鏄暣鏁 (15)
$foo = 5 + "10 Small Pigs"; // $foo 鏄暣鏁 (15)
?>
濡傛灉涓婇潰涓や釜渚嬪瓙鐪嬩笂鍘诲彜鎬殑璇濓紝鍙傝瀛楃涓茶浆鎹负鏁板笺
濡傛灉瑕佸己鍒跺皢涓涓彉閲忓綋浣滄煇绉嶇被鍨嬫潵姹傚硷紝鍙傝绫诲瀷寮哄埗杞崲涓鑺傘傚鏋滆鏀瑰彉涓涓彉閲忕殑绫诲瀷锛屽弬瑙 settype()銆
濡傛灉鎯宠娴嬭瘯鏈妭涓换浣曚緥瀛愮殑璇濓紝鍙互鐢 var_dump() 鍑芥暟銆
Note:
鑷姩杞崲涓 鏁扮粍 鐨勮涓虹洰鍓嶆病鏈夊畾涔夈
Also, because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions:
<?php
$a = 'car'; // $a is a string
$a[0] = 'b'; // $a is still a string
echo $a; // bar
?>
璇峰弬闃呰闂拰淇敼瀛楃涓蹭腑鐨勫瓧绗︿竴鑺備互鑾峰彇鏇村淇℃伅銆
绫诲瀷寮哄埗杞崲
PHP 涓殑绫诲瀷寮哄埗杞崲鍜 C 涓殑闈炲父鍍忥細鍦ㄨ杞崲鐨勫彉閲忎箣鍓嶅姞涓婄敤鎷彿鎷捣鏉ョ殑鐩爣绫诲瀷銆
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
鍏佽鐨勫己鍒惰浆鎹㈡湁锛
(int), (integer) - 杞崲涓 鏁村瀷(integer)
(bool), (boolean) - 杞崲涓 甯冨皵鍨(boolean)
(float), (double), (real) - 杞崲涓 娴偣鍨(float)
(string) - 杞崲涓 瀛楃涓(string)
(binary) - 杞崲涓轰簩杩涘埗 瀛楃涓(string) (PHP 6)
(array) - 杞崲涓 鏁扮粍(array)
(object) - 杞崲涓 瀵硅薄(object)
(unset) - 杞崲涓 NULL (PHP 5)
(binary) 杞崲浼氬湪缁撴灉鍓嶉潰鍔犱笂鍓嶇紑'b'锛孭HP 5.2.1 鏂板銆
娉ㄦ剰鍦ㄦ嫭鍙峰唴鍏佽鏈夌┖鏍煎拰鍒惰〃绗︼紝鎵浠ヤ笅闈袱涓緥瀛愬姛鑳界浉鍚岋細
<?php
$foo = (int) $bar;
$foo = ( int ) $bar;
?>
灏 瀛楃涓(string)鏂囧瓧鍜屽彉閲忚浆鎹负浜岃繘鍒 瀛楃涓(string)锛
<?php
$binary = (binary)$string;
$binary = b"binary string";
?>
Note:
鍙互灏嗗彉閲忔斁缃湪鍙屽紩鍙蜂腑鐨勬柟寮忔潵浠f浛灏嗗彉閲忚浆鎹㈡垚 瀛楃涓(string)s锛
<?php
$foo = 10; // $foo 鏄竴涓暣鏁
$str = "$foo"; // $str 鏄竴涓瓧绗︿覆
$fst = (string) $foo; // $fst 涔熸槸涓涓瓧绗︿覆
// 杈撳嚭 "they are the same"
if ($fst === $str) {
echo "they are the same";
}
?>
鏈夋椂鍦ㄧ被鍨嬩箣闂村己鍒惰浆鎹㈡椂纭垏鍦颁細鍙戠敓浠涔堝彲鑳戒笉鏄緢鏄庢樉銆
浠ヤ笂灏辨槸PHP绫诲瀷涔嬬被鍨嬭浆鎹㈢殑鍒ゅ埆鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒