如果另一个字符串为空,如何在PHP中将字符串的默认值设置为?

时间:2021-10-22 22:52:48

Best example would be to show you how is this solved in Javascript:

最好的例子是向您展示如何在Javascript中解决这个问题:

var someString = someEmptyString || 'new text value';

In this javascript example, we have detected that 'someEmptyString' is empty and automatically set the value to 'new text value'. Is this possible in PHP and what's the shortest (code) way to do it?

在这个javascript示例中,我们检测到'someEmptyString'为空并自动将值设置为'new text value'。这在PHP中是可行的吗?最简单的(代码)方法是什么?

This is how I do it now:

这就是我现在的做法:

if ($someEmptyString == "")
    $someString = 'new text value'; else $someString = $someEmptyString;

This is bugging me for quite some time and I would be very grateful if someone knows a better way to do this. Thank you!

这让我困扰了很长一段时间,如果有人知道更好的方法,我会非常感激。谢谢!

4 个解决方案

#1


38  

You can use the ternary operator ?:.

你可以使用三元运算符吗?:。

If you have PHP 5.3, this is very elegant:

如果你有PHP 5.3,这是非常优雅的:

$someString = $someEmptyString ?: 'new text value';

Before 5.3, it needs to be a bit more verbose:

在5.3之前,它需要更冗长:

$someString = $someEmptyString ? $someEmptyString : 'new text value';

#2


3  

$someString = (!isSet( $someEmptyString ) || empty( $someEmptyString ) )? "new text value" : $someEmptyString;

I think that would be the most correct way to do this. Check if that var is empty or if it's not set and run condition.

我认为这是最正确的方法。检查var是否为空或者是否未设置和运行条件。

it's still a bit of code, but you shouldn't get any PHP warnings or errors when executed.

它仍然是一些代码,但你不应该在执行时得到任何PHP警告或错误。

#3


1  

You can use the ternary operator

您可以使用三元运算符

$someString = $someEmptyString ?: "New Text Value";

#4


0  

While ternary is more clear whats happening, You can also set the variable like this

虽然三元更清楚发生了什么,但您也可以像这样设置变量

($someString = $someEmptyString) || ($someString = "Default");

Works in all PHP versions, You can extend this to use more than 1 option failry easily also

适用于所有PHP版本,您可以将其扩展为使用多个选项失败也很容易

($someString = $someEmptyString) ||
($someString = $someOtherEmptyString) ||
($someString = "Default");

which I personally find more readable than its ternary equivalent

我个人发现它比三元等价物更具可读性

#1


38  

You can use the ternary operator ?:.

你可以使用三元运算符吗?:。

If you have PHP 5.3, this is very elegant:

如果你有PHP 5.3,这是非常优雅的:

$someString = $someEmptyString ?: 'new text value';

Before 5.3, it needs to be a bit more verbose:

在5.3之前,它需要更冗长:

$someString = $someEmptyString ? $someEmptyString : 'new text value';

#2


3  

$someString = (!isSet( $someEmptyString ) || empty( $someEmptyString ) )? "new text value" : $someEmptyString;

I think that would be the most correct way to do this. Check if that var is empty or if it's not set and run condition.

我认为这是最正确的方法。检查var是否为空或者是否未设置和运行条件。

it's still a bit of code, but you shouldn't get any PHP warnings or errors when executed.

它仍然是一些代码,但你不应该在执行时得到任何PHP警告或错误。

#3


1  

You can use the ternary operator

您可以使用三元运算符

$someString = $someEmptyString ?: "New Text Value";

#4


0  

While ternary is more clear whats happening, You can also set the variable like this

虽然三元更清楚发生了什么,但您也可以像这样设置变量

($someString = $someEmptyString) || ($someString = "Default");

Works in all PHP versions, You can extend this to use more than 1 option failry easily also

适用于所有PHP版本,您可以将其扩展为使用多个选项失败也很容易

($someString = $someEmptyString) ||
($someString = $someOtherEmptyString) ||
($someString = "Default");

which I personally find more readable than its ternary equivalent

我个人发现它比三元等价物更具可读性