无法解释的逻辑 OR ( || ) 语法错误 [英] Unexplained syntax error with logical OR ( || )

查看:69
本文介绍了无法解释的逻辑 OR ( || ) 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用适用于 Mac 的 NetBeans,并且在 Linux 的共享主机中运行 CakePHP(尽管我认为该框架与它无关).这不是什么大问题,但令人沮丧.

I'm working with NetBeans for Mac, and I'm running CakePHP (though I don't think the framework has anything to do with it) in a shared hosting in Linux. This is not a big issue, but it is frustrating.

我想知道为什么我不能简单地做到这一点:

I wonder why I can't simply do this:

if($this->Session->read('User.value1') || $this->Session->read('User.value2')){
  ...
}

我得到的错误信息是:

Error: syntax error, unexpected '$this' (T_VARIABLE)

为什么会出现语法错误?我看不到.

Why is there a syntax error? I can't see it.

我可以毫无问题地做到这一点:

I can do this with no problems:

if($this->Session->read('value1')){
  ...
}

我也可以毫无问题地做到这一点(|| 周围没有空格):

I can also do this with no problems (no whitespace around ||):

if($this->Session->read('User.value1')||$this->Session->read('User.value2')){
  ...
}

但是如果我在 || 操作符周围加上空格,它就会停止工作.或者更确切地说—这是最令人困惑的部分—有时当我在 || 运算符周围放置空格时它会停止工作,有时它不会.

But if I put spaces around the || operator, it stops working. Or rather — and this is the most confusing part — sometimes it stops working when I put spaces around the || operator, and sometimes it doesn't.

我认为这可能是 Netbeans 7.4 中的一个错误,但是当我忽略来自 NetBeans 的警告并尝试运行代码时,PHP 给了我同样的错误.

I thought this might be a bug in Netbeans 7.4, but when I ignored the warning from NetBeans and tried to run the code anyway, PHP gave me the same error.

这里发生了什么?

推荐答案

我正在使用适用于 MAC 的 NetBeans

I'm working With NetBeans for MAC

什么时候空格不是空格?

当它是一个不间断空间时

目的是:

" || "
207C7C20 (hex)

但实际上源文件中的内容几乎可以肯定:

But what is actually in the source file is almost certainly:

" || "
207C7CA0 (hex)

(在堆栈溢出时不会,但我敢打赌它在源文件中).

(on stack overflow it won't be but I bet it is in the source file).

对于 Mac,问题是(使用我自己的键盘布局,但我假设它与您的情况类似):

With a mac the problem is (using my own keyboard layout, but I am assuming it's similar in your case):

"|" = alt + 1
" " = alt + space (accidental)

所以打字,序列"||"当按下空格键时,很容易仍然按下 alt 键,并且:瞧,你会得到意想不到的wat"表面上没有意义的语法错误 - 直到您意识到问题所在.

So typing away, with the sequence " || " it's very easy for the alt key to still be depressed when the space bar is pressed and: voilà you get unexpected "wat" syntax errors which at face value make no sense - until you realize what the problem is.

示例:

-> cat foo.php 
<?php

$foo = "x";
if (true || $foo) {
}
-> php -l foo.php 

Parse error: syntax error, unexpected '$foo' (T_VARIABLE) in foo.php on line 4

Errors parsing foo.php

这篇关于无法解释的逻辑 OR ( || ) 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆