Parse error using multiple `or` conditions

90 Views Asked by At

I am having issues using multiple or conditions.

Here the code, very simple :

<?php

$a = "a";

if($a == "a" || $a == "b" || $a == "c" || $a == "d" || $a == "e")
{echo "test";}

?>

But PHP displays this error :

Parse error: syntax error, unexpected '$a' (T_VARIABLE) in /home/guest/public_html/or.php on line 5

2

There are 2 best solutions below

4
On BEST ANSWER

So I was right in the comments: it's a (yet another) Case of Invisible Characters. Here's what's really stored in your file:

if($a == "a" ||\u00A0$a == "b" ||\u00A0$a == "c" || $a == "d" || $a == "e")

As you see, in two places $a is preceded not with a normal whitespace, but with something else - a special character, so called Non-breaking space one. It's not visually different from a normal whitespace, and it's truncated by StackOverflow engine when you paste the code into the question. But it messes up how the parser interprets your code - hence an error.

1
On

I'm assuming this is an excerpt of your code, and the line 5 is (most probably) the line with $a="a";. Check for an incomplete {} or a missing ; prior to that line.