Prints value of array or object according to key value if exists else throws exception

PHOTO EMBED

Thu Mar 10 2022 05:52:14 GMT+0000 (Coordinated Universal Time)

Saved by @dharmesh #php

function kPrint($key,$obj){
return (gettype($obj)=="array"?(array_key_exists($key,$obj)?$obj[$key]:("<font color='red'>NA</font>")):(gettype($obj)=="object"?(property_exists($obj,$key)?$obj->$key:("<font color='red'>NA</font>")):("<font color='red'>
    <font color='green'>:::Exception Start:::</font><br>Invalid Object/Array Passed at kPrint() Function!!<br> At : Variable => ".print_r($obj)."<br>Key => ".$key."<br> At File: <font color='blue'>".debug_backtrace()[0]['file']."</font><br> At Line : ".debug_backtrace()[0]['line']."<br>
    <font color='green'>:::Exception End:::</font>
</font>")));
}
content_copyCOPY

this can be used while printing values from array according to key if it exists else throws NA and if passed object is not valid then throws exception error.