PHP:如果不为空则赋值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4696453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:00:27  来源:igfitidea点击:

PHP: Assign if not empty?

php

提问by Peter

Is there any sort of assign-if-not-empty-otherwise-assign-null function in PHP?

PHP中是否有任何类型的assign-if-not-empty-otherwise-assign-null函数?

I'm looking for a cleaner alternative to the following:

我正在寻找以下更清洁的替代方案:

$variable = (!empty($item)) ? $item : NULL;

It would also be handy if I could specify the default value; for instance, sometimes I'd like ' ' instead of NULL.

如果我可以指定默认值也会很方便;例如,有时我想要 ' ' 而不是 NULL。

I could write my own function, but is there a native solution?

我可以编写自己的函数,但是有本地解决方案吗?

Thanks!

谢谢!

EDIT: It should be noted that I'm trying to avoid a notice for undefined values.

编辑:应该注意的是,我试图避免对未定义值的通知。

回答by BoltClock

Re edit:unfortunately, both generate notices on undefined variables. You could counter that with @, I guess.

重新编辑:不幸的是,两者都会对未定义的变量生成通知。你可以用 来反驳@,我猜。

In PHP 5.3 you can do this:

在 PHP 5.3 中,你可以这样做:

$variable = $item ?: NULL;

Or you can do this (as meagar says):

或者你可以这样做(正如 meagar 所说):

$variable = $item ? $item : NULL;

Otherwise no, there isn't any other way.

否则,没有其他办法。

回答by Peter

Update

更新

PHP 7 adds a new featureto handle this.

PHP 7 添加了一个新功能来处理这个问题。

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

空合并运算符 (??) 已被添加为语法糖,用于需要将三元与 isset() 结合使用的常见情况。如果存在且不为 NULL,则返回其第一个操作数;否则它返回它的第二个操作数。

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

Original Answer

原答案

I ended up just creating a function to solve the problem:

我最终只是创建了一个函数来解决这个问题:

public function assignIfNotEmpty(&$item, $default)
{
    return (!empty($item)) ? $item : $default;
}

Note that $item is passed by referenceto the function.

请注意, $item 是通过引用传递给函数的。

Usage example:

用法示例:

$variable = assignIfNotEmpty($item, $default);

回答by mario

Well yes, there is a native solution for assigning the value or NULL when the variable was unset:

是的,当变量未设置时,有一个本地解决方案用于分配值或 NULL:

$variable = $possibly_unset_var;

If you just want to suppress the notice (which doesn't solve anything or makes the code cleaner), there is also a native syntax for that.

如果您只是想取消通知(这不能解决任何问题或使代码更清晰),那么还有一个本机语法。

$variable = @$unset_var;

回答by Dennis Kreminsky

I wouldn't recommend this on a production system, but:

我不会在生产系统上推荐这个,但是:

<?php
//$value=1;
$item=@$value ?:null;
var_dump($item); // NULL
?>

<?php
$value=1;
$item=@$value ?:null;
var_dump($item); // 1
?>