PHP 判断函数、类、方法、属性、数组中的 key 是否存在

PHP About 821 words

判断函数是否存在

判断系统函数或自己写的函数是否存在

bool function_exists(string $function_name)

if(function_exists('curl_init')) {
    curl_init();
} else {
    echo 'not function curl_init';
}

判断类是否存在

bool class_exists(string $class_name [, bool $autoload = true])

if(class_exists('MySQL')) {
    $myclass=new MySQL();
}

判断类的属性是否存在

bool property_exists(mixed $class,string $property)

$directory=new Directory;
if(!property_exists($directory,'li')){
    echo '未定义li属性!';
}

判断类的方法是否存在

bool method_exists(mixed $object, string $method_name)

$directory=new Directory;
if(!method_exists($directory,'read')) {
    echo '未定义read方法!';
}

判断数组中的 key 是否存在

isset()

$arr = ['aaa' => 'bbb']
if(isset($arr['aaa'])) {
    echo '存在aaa角标';
}

array_key_exists($key, $array)

$arr = ['aaa' => 'bbb']
if(array_key_exists('aaa', $arr)) {
    echo '存在aaa角标';
}
Views: 3,965 · Posted: 2019-04-08

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh