Is there a shortcut for the “isset construct”?

谁说胖子不能爱 提交于 2019-12-12 09:34:30

问题


I'm writing quite often this line of code:

$myParam = isset($params['myParam']) ? $params['myParam'] : 'defaultValue';

Typically, it makes the line very long for nested arrays.

Can I make it shorter?


回答1:


function getOr(&$var, $default) {
    if (isset($var)) {
        return $var;
    } else {
        return $default;
    }
}

$myParam = getOr($params['myParam'], 'defaultValue');

Be sure to pass the variable by reference though, otherwise the code will produce a E_NOTICE. Also the use of if/else instead of a ternary operator is intentional here, so the zval can be shared if you are using PHP < 5.4.0RC1.




回答2:


Yes, by making a proxy function, but is it really worth it?

Also, isset is a language construct, so wrapping it in a proxy function will degrade performance, although the degradation will likely be less than trivial (not even really worth mentioning.)




回答3:


PHP 7 will contain ?? operator that does exactly that.

See https://wiki.php.net/rfc/isset_ternary, example:

// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';



回答4:


This is what I use:

function getindex($arr, $index, $default = null) {
    return isset($arr[$index]) ? $arr[$index] : $default;
}



回答5:


As of PHP 5.3 you can use:

$myParam = $params['myParam'] ?: 'defaultValue';

Note, however, that $params['myParam'] and isset($params['myParam']) are not 100% the same.




回答6:


I'm using little this little magic class which works as variable

class Post() {
 private $post = Array();
 public function __construct() {
  $this->post = $_POST;
 }
 public function __get($name) {
  return @$this->post[$name];
 }
 public function __set($name, $value) {
  return $this->post[$name] = $value;
 }
 public function __call($function, $params) {
  if(isset($this->post[$function])) {
   return $this->post[$function];
  } else {
   $this->post[$function] = $params[0];
   return $params[0];
  }
 }
}
$post = new Post();

then in document you can use it easily as any other variable so for example $post->name $post->somelist[2] or with default value $post->name("John Doe") and after that you got it returned as well as stored.




回答7:


I know this doesn't shorten anything up for you but thought I'd just share this, I use this alot in my applications to make sure something is set and has a value.

function is_blank($var = NULL){
    return empty($var) && !is_numeric($var) && !is_bool($var);
}    

function chk_var($var = NULL){
    return (isset($var) && !is_null($var) && !is_blank($var));
}

Then...

if(chk_var($myvar)){ ... }



回答8:


No. Unfortunately, you can't. Not in a decent way. You'll at least have to give in on performance.

Update: since PHP7, ?? will do just that. See https://wiki.php.net/rfc/isset_ternary




回答9:


You if you have to do it often, you are probably missing the point.

In fact, variables should be defined before use.
So, there oughtn't be a case when you have your param undefined.
Just create a default params file, and initialize every your variable.

$params['myParam'] = 'defaultValue'; 

later it can be changed under some circunstances but it never be undefined.
Got the idea?



来源:https://stackoverflow.com/questions/8216594/is-there-a-shortcut-for-the-isset-construct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!