PHP姣旇緝杩愮畻绗

姣旇緝杩愮畻绗︼紝濡傚悓瀹冧滑鍚嶇О鎵鏆楃ず鐨勶紝鍏佽瀵逛袱涓艰繘琛屾瘮杈冦傝繕鍙互鍙傝 PHP 绫诲瀷姣旇緝琛ㄧ湅涓嶅悓绫诲瀷鐩镐簰姣旇緝鐨勪緥瀛愩

姣旇緝杩愮畻绗
渚嬪瓙    鍚嶇О    缁撴灉
$a == $b    绛変簬    TRUE锛屽鏋 $a 绛変簬 $b銆
$a === $b    鍏ㄧ瓑    TRUE锛屽鏋 $a 绛変簬 $b锛屽苟涓斿畠浠殑绫诲瀷涔熺浉鍚屻傦紙PHP 4 寮曡繘锛
$a != $b    涓嶇瓑    TRUE锛屽鏋 $a 涓嶇瓑浜 $b銆
$a <> $b    涓嶇瓑    TRUE锛屽鏋 $a 涓嶇瓑浜 $b銆
$a !== $b    闈炲叏绛    TRUE锛屽鏋 $a 涓嶇瓑浜 $b锛屾垨鑰呭畠浠殑绫诲瀷涓嶅悓銆傦紙PHP 4 寮曡繘锛
$a < $b    灏忎笌    TRUE锛屽鏋 $a 涓ユ牸灏忎簬 $b銆
$a > $b    澶т簬    TRUE锛屽鏋 $a 涓ユ牸 $b銆
$a <= $b    灏忎簬绛変簬    TRUE锛屽鏋 $a 灏忎簬鎴栬呯瓑浜 $b銆
$a >= $b    澶т簬绛変簬    TRUE锛屽鏋 $a 澶т簬鎴栬呯瓑浜 $b銆
濡傛灉姣旇緝涓涓暣鏁板拰瀛楃涓诧紝鍒欏瓧绗︿覆浼氳杞崲涓烘暣鏁般傚鏋滄瘮杈冧袱涓暟瀛楀瓧绗︿覆锛屽垯浣滀负鏁存暟姣旇緝銆傛瑙勫垯涔熼傜敤浜 switch 璇彞銆 <?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>

瀵逛簬澶氱绫诲瀷锛屾瘮杈冭繍绠楃鏍规嵁涓嬭〃姣旇緝锛堟寜椤哄簭锛夈

姣旇緝澶氱绫诲瀷
杩愮畻鏁 1 绫诲瀷    杩愮畻鏁 1 绫诲瀷    缁撴灉
null 鎴 string    string    灏 NULL 杞崲涓 ""锛岃繘琛屾暟瀛楁垨璇嶆眹姣旇緝
bool 鎴 null    浠讳綍鍏跺畠绫诲瀷    杞崲涓 bool锛孎ALSE < TRUE
object    object    鍐呯疆绫诲彲浠ュ畾涔夎嚜宸辩殑姣旇緝锛屼笉鍚岀被涓嶈兘姣旇緝锛岀浉鍚岀被鍜屾暟缁勫悓鏍锋柟寮忔瘮杈冨睘鎬э紙PHP 4 涓級锛孭HP 5 鏈夊叾鑷繁鐨勮鏄
string锛宺esource 鎴 number    string锛宺esource 鎴 number    灏嗗瓧绗︿覆鍜岃祫婧愯浆鎹㈡垚鏁板瓧锛屾寜鏅氭暟瀛︽瘮杈
array    array    鍏锋湁杈冨皯鎴愬憳鐨勬暟缁勮緝灏忥紝濡傛灉杩愮畻鏁 1 涓殑閿笉瀛樺湪浜庤繍绠楁暟 2 涓垯鏁扮粍鏃犳硶姣旇緝锛屽惁鍒欐尐涓兼瘮杈冿紙瑙佷笅渚嬶級
array    浠讳綍鍏跺畠绫诲瀷    array 鎬绘槸鏇村ぇ
object    浠讳綍鍏跺畠绫诲瀷    object 鎬绘槸鏇村ぇ

Example #1 鏍囧噯鏁扮粍姣旇緝浠g爜

<?php
// 鏁扮粍鏄敤鏍囧噯姣旇緝杩愮畻绗﹁繖鏍锋瘮杈冪殑
function standard_array_compare($op1, $op2)
{
    if (count($op1) < count($op2)) {
        return -1; // $op1 < $op2
    } elseif (count($op1) > count($op2)) {
        return 1; // $op1 > $op2
    }
    foreach ($op1 as $key => $val) {
        if (!array_key_exists($key, $op2)) {
            return null; // uncomparable
        } elseif ($val < $op2[$key]) {
            return -1;
        } elseif ($val > $op2[$key]) {
            return 1;
        }
    }
    return 0; // $op1 == $op2
}
?>
鍙傝 strcasecmp()锛宻trcmp()锛屾暟缁勮繍绠楃鍜岀被鍨嬩竴绔犮

涓夊厓杩愮畻绗
鍙︿竴涓潯浠惰繍绠楃鏄?:鈥濓紙鎴栦笁鍏冿級杩愮畻绗 銆

Example #2 璧嬮粯璁ゅ

<?php
 // Example usage for: Ternary Operator
 $action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

 // The above is identical to this if/else statement
 if (empty($_POST['action'])) {
     $action = 'default';
 } else {
     $action = $_POST['action'];
 }

 ?> 琛ㄨ揪寮 (expr1) ? (expr2) : (expr3) 鍦 expr1 姹傚间负 TRUE 鏃剁殑鍊间负 expr2锛屽湪 expr1 姹傚间负 FALSE 鏃剁殑鍊间负 expr3銆
Note: 娉ㄦ剰涓夊厓杩愮畻绗︽槸涓鍙ワ紝鍥犳鍏舵眰鍊间笉鏄彉閲忥紝鑰屾槸璇彞鐨勭粨鏋溿傚鏋滄兂閫氳繃寮曠敤杩斿洖涓涓彉閲忚繖鐐瑰氨寰堥噸瑕併傚湪涓涓氳繃寮曠敤杩斿洖鐨勫嚱鏁颁腑璇彞 return $var == 42 ? $a : $b; 灏嗕笉璧蜂綔鐢紝浠ュ悗鐨 PHP 鐗堟湰浼氫负姝ゅ彂鍑轰竴鏉¤鍛娿

Note:

寤鸿閬垮厤灏嗕笁鍏冭繍绠楃鍫嗙Н鍦ㄤ竴璧蜂娇鐢ㄣ傚綋鍦ㄤ竴鏉¤鍙ヤ腑浣跨敤澶氫釜涓夊厓杩愮畻绗︽椂浼氶犳垚 PHP 杩愮畻缁撴灉涓嶆竻鏅帮細 Is is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:

Example #3 涓嶆竻鏅扮殑涓夊厓杩愮畻绗﹁涓

<?php
// 涔嶇湅璧锋潵涓嬮潰鐨勮緭鍑烘槸 'true'
echo (true?'true':false?'t':'f');

// 鐒惰岋紝涓婇潰璇彞鐨勫疄闄呰緭鍑烘槸't'锛屽洜涓轰笁鍏冭繍绠楃鏄粠宸﹀線鍙宠绠楃殑

// 涓嬮潰鏄笌涓婇潰绛変环鐨勮鍙ワ紝浣嗘洿娓呮櫚
echo ((true ? 'true' : 'false') ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

浠ヤ笂灏辨槸PHP姣旇緝杩愮畻绗︾殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=65