“isset 构造”有捷径吗?

发布于 2024-12-17 02:05:51 字数 169 浏览 1 评论 0原文

我经常写这行代码:

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

通常,对于嵌套数组来说,它会使该行变得很长。

我可以把它改短一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

白龙吟 2024-12-24 02:05:51
function getOr(&$var, $default) {
    if (isset($var)) {
        return $var;
    } else {
        return $default;
    }
}

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

但请务必通过引用传递变量,否则代码将产生 E_NOTICE。另外,这里有意使用 if/else 而不是三元运算符,因此如果您使用 PHP <<,则可以共享 zval。 5.4.0RC1。

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.

蓝咒 2024-12-24 02:05:51

PHP 7 将包含正是这样做的 ?? 运算符。

请参阅 https://wiki.php.net/rfc/isset_ternary,示例:

// 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';

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';
山川志 2024-12-24 02:05:51

是的,通过制作代理功能,但这真的值得吗?

此外,isset 是一种语言构造,因此将其包装在代理函数中将会降低性能,尽管这种降低可能不是微不足道的(甚至不值得一提)。

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.)

雪花飘飘的天空 2024-12-24 02:05:51

这就是我使用的:

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

This is what I use:

function getindex($arr, $index, $default = null) {
    return isset($arr[$index]) ? $arr[$index] : $default;
}
仅冇旳回忆 2024-12-24 02:05:51

从 PHP 5.3 开始,您可以使用:

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

但是请注意,$params['myParam']isset($params['myParam']) 并不是 100%相同的。

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.

半暖夏伤 2024-12-24 02:05:51

我使用了这个小魔法类,它作为变量工作,

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();

然后在文档中您可以轻松地将它用作任何其他变量,例如 $post->name $post->somelist [2] 或使用默认值 $post->name("John Doe") ,之后您将其返回并存储。

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.

情场扛把子 2024-12-24 02:05:51

我知道这不会为您缩短任何内容,但我想我只是分享这个,我在我的应用程序中经常使用它来确保某些内容已设置并具有值。

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));
}

然后...

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

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)){ ... }
执妄 2024-12-24 02:05:51

不,不幸的是,你不能。不是以一种体面的方式。你至少必须在性能上做出让步。

更新:从 PHP7 开始,?? 就可以做到这一点。请参阅https://wiki.php.net/rfc/isset_ternary

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

入画浅相思 2024-12-24 02:05:51

如果你必须经常这样做,那么你可能没有抓住重点。

事实上,变量应该在使用之前定义
因此,不应该出现参数未定义的情况。
只需创建一个默认的参数文件,并初始化每个变量。

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

稍后在某些情况下可以更改它,但它永远不会是未定义的。
明白了吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文