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

发布于 2024-11-16 15:58:38 字数 410 浏览 2 评论 0原文

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

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

在这个 javascript 示例中,我们检测到“someEmptyString”为空,并自动将该值设置为“新文本值”。这在 PHP 中可能吗?最短的(代码)方法是什么?

这就是我现在的做法:

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

这困扰了我很长一段时间,如果有人知道更好的方法来做到这一点,我将非常感激。谢谢你!

Best example would be to show you how is this solved in 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?

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!

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

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

发布评论

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

评论(4

我不是你的备胎 2024-11-23 15:58:38

您可以使用 三元运算符 ?:< /a>.

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

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

在 5.3 之前,它需要更详细一点:

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

You can use the ternary operator ?:.

If you have PHP 5.3, this is very elegant:

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

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

$someString = $someEmptyString ? $someEmptyString : 'new text value';
仅此而已 2024-11-23 15:58:38
$someString = (!isSet( $someEmptyString ) || empty( $someEmptyString ) )? "new text value" : $someEmptyString;

我认为这将是最正确的方法。检查该变量是否为空或者是否未设置并运行条件。

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

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

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

唱一曲作罢 2024-11-23 15:58:38

您可以使用三元运算符

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

You can use the ternary operator

$someString = $someEmptyString ?: "New Text Value";
旧话新听 2024-11-23 15:58:38

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

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

适用于所有 PHP 版本,您可以扩展它以轻松使用超过 1 个选项失败,

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

我个人认为这比其三元等效项更具可读性

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

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

which I personally find more readable than its ternary equivalent

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