Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplained syntax error with logical OR ( || )

Tags:

php

netbeans

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')){
  ...
}

The error message I get is:

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.

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.

What is happening here?

like image 561
Dropial Avatar asked May 14 '14 17:05

Dropial


People also ask

What is an example of a logical error?

A logical error in a program is an error were the instructions given in the program do not accomplish the intended goal. "Get me a cup of coffee." is a logical error when the person intended to ask for a cup of tea. In computer programs, this error can occur in many different forms.

Why do I keep getting a syntax error?

A syntax error occurs when a programmer writes an incorrect line of code. Most syntax errors involve missing punctuation or a misspelled name. If there is a syntax error in a compiled or interpreted programming language, then the code won't work.

What causes Python syntax error?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.


1 Answers

I'm working With NetBeans for MAC

When is a space not a space?

When it's a non-breaking space!

The intention is:

" || "
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).

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)

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.

Example:

-> 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
like image 144
AD7six Avatar answered Nov 15 '22 16:11

AD7six