PHP 和 JS 变量求值的区别

发布于 2024-10-25 11:59:57 字数 350 浏览 2 评论 0原文

有人可以向我解释一下为什么下面的 JavaScript 代码会生成 321 警报,而 PHP 代码会生成 1。

我知道 PHP 代码会计算表达式并返回 true 或 false。我不知道为什么在 JavaScript 中它像三元运算符一样工作。这只是语言中实现事物的方式吗?

var something = false;
var somethingelse = (something || 321);
alert(somethingelse); // alerts 321
$var = '123';
$other = ($var || 321);
echo $other; // prints 1

谢谢!

Can someone please explain to me why the following javascript code produces an alert with 321 and the PHP code produces 1.

I know the PHP code evaluates the expression and returns true or false. What I don't know is why in JavaScript it works like a ternary operator. Is it just the way things were implemented in the language?

var something = false;
var somethingelse = (something || 321);
alert(somethingelse); // alerts 321
$var = '123';
$other = ($var || 321);
echo $other; // prints 1

Thanks!

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

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

发布评论

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

评论(4

清风不识月 2024-11-01 11:59:57

这只是语言的实现方式吗?

是的,JavaScript 的做法有点不同。表达式 (something || 321) 表示 something 是否属于 falsy 值,使用默认值 321 代替。

在条件表达式中,|| 像往常一样充当逻辑OR,但实际上它执行相同的合并操作。您可以使用以下代码对此进行测试:

if ((0 || 123) === true)
    alert('0 || 123 evaluates to a Boolean');
else
    alert('0 || 123 does not evaluate to a Boolean');

在 PHP 中,|| 运算符执行逻辑 OR 并给出布尔结果,仅此而已。

Is it just the way things were implemented in the language?

Yes, JavaScript does it a bit differently. The expression (something || 321) means if something is of a falsy value, a default value of 321 is used instead.

In conditional expressions || acts as a logical OR as usual, but in reality it performs the same coalescing operation. You can test this with the following:

if ((0 || 123) === true)
    alert('0 || 123 evaluates to a Boolean');
else
    alert('0 || 123 does not evaluate to a Boolean');

In PHP the || operator performs a logical OR and gives a Boolean result, nothing else.

坏尐絯 2024-11-01 11:59:57

我实际上很惊讶 javascript 也没有警报 1 或 true 。你想要的 js 语法是:

var somethingelse = something || 321;

用括号括起来,将其评估为 true / falsey。对于 php 你是说:

//$other will equal true if $var is true or 321 is true. 
$other = ($var || 321);

php 中的匹配语句如下所示:

$other = ($var) ? $var : 321;

I'm actually surprised javascript did not alert 1 or true as well. The syntax you want for js is:

var somethingelse = something || 321;

Wrapping parentheses around something evaluates it as truthy / falsey. For php your are saying:

//$other will equal true if $var is true or 321 is true. 
$other = ($var || 321);

A matching statement in php would look like:

$other = ($var) ? $var : 321;
罪歌 2024-11-01 11:59:57

只是添加 BoltClock 答案,因为我无法发表评论 - 如果您希望它是一个布尔值,您可以将其解析为 bool ,如下所示:

var somthing = !!(somthingelse || 321);

Just to add on boltClock answer since I can't comment - If you want it be a Boolean value you can parse it to bool like this:

var somthing = !!(somthingelse || 321);
困倦 2024-11-01 11:59:57

在 PHP 中,($var || 321); 被计算并分配给 $other

您可以在 PHP 中使用它。

($other = $var) || $other = 321;

更新:正如 BoltClock 在 Javascript 中所说,如果 something 为 false,var Somethingelse = (something || 321) 会寻求为变量分配一个默认值。

In PHP ($var || 321); is evaluated and assigned to $other.

You can use this in PHP.

($other = $var) || $other = 321;

Update: As BoltClock said in Javascript var somethingelse = (something || 321) seeks to assign a default value to the variable if something is false.

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