Anyone ever used PHP's (unset) casting?

前端 未结 3 2113
陌清茗
陌清茗 2021-02-18 13:09

I just noticed PHP has an type casting to (unset), and I\'m wondering what it could possibly be used for. It doesn\'t even really unset the variable, it just casts

3条回答
  •  伪装坚强ぢ
    2021-02-18 13:15

    For example it can be used like this

    function fallback()
    {
        // some stuff here
        return 'zoo';
    }
    
    var_dump(false ? 'foo' : fallback()); // zoo
    var_dump(false ? 'foo' : (unset) fallback()); // null
    

    Even if fallback() returns "zoo" (unset) will clear that value.

提交回复
热议问题