如果isset,则返回PHP [英] PHP return if isset
问题描述
是否有如下所示的内置php函数:
Is there a built-in php function like the following:
function isset_get($array, $key, $default = null) {
return isset($array[$key]) ? $array[$key] : $default;
}
我不喜欢
$my_var = isset($my_array['some Key']) ? $my_array['some Key'] : '';
,并且希望
$my_var = isset_get($my_array, 'some Key', '');
或类似的东西...
推荐答案
否。在我的代码库中,我们有几个这种性质的助手。名称非常可怕,但是由于它们经常使用,因此必须保证简洁性
No. In my codebase we have several helpers of this nature. The names are pretty terrible but since they are frequently used, concision is warranted
idx($ array,$ key,$ default)
如果!isset($ array [$ key])
idx($array, $key, $default)
returns the $default if !isset($array[$key])
adx($ array,$键,$ default)
类似于idx,但强制$ array实际上是一个数组,如果不是,则抛出该错误。
adx($array, $key, $default)
like idx, but enforces that $array is actually an array and throws if not.
edx($ array,$ key,$ default)
如果!isset($ array [$ key])或为空($ array [$ key] )
edx($array, $key, $default)
returns the $default if !isset($array[$key]) or empty($array[$key])
这篇关于如果isset,则返回PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!