SoWizardly
SoWizardly

Reputation: 407

Simple PHP If Statement error

I have a very simple IF statement...

if ($key == "listingURL" or 
     $key == "interiorColor" or 
     $key == "engine" or 
     $key == "transmission" or 
     $key == "stockNumber" or 
     $key == "VIN") {
          // Do thing
}

But I'm receiving an error...

[23-Apr-2015 13:12:01 UTC] PHP Parse error: syntax error, unexpected T_VARIABLE in xxx on line xxx

Which is this line...

 $key == "stockNumber" or

Is there a limit to the maximum amount of OR's, or am I missing something staring me right in the face?

Upvotes: 5

Views: 1021

Answers (5)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"Is there a limit to the maximum amount of OR's, or am I missing something staring me right in the face?"

No there isn't. The reason is because you have a hidden character:

$key == "transmission" or ? <= right there

enter image description here

Which is &#65279;

Being a Unicode ZERO WIDTH NO-BREAK SPACE character.

Rewrite:

if ($key == "listingURL" or 
     $key == "interiorColor" or 
     $key == "engine" or 
     $key == "transmission" or
     $key == "stockNumber" or 
     $key == "VIN") {
          // Do thing
}

Sidenotes:

As from the comments:

I'll confirm this as the correct answer as soon as the time limit is up! Thank you so much for the help. I use Sublime Text 3, is there some easy way to detect these hidden characters? – SoWizardly 19 mins ago

For Notepad++ there is a plugin called: HEX-Editor.

You can download it via: Extensions -> Plugin Manager -> Available. Just check the combo box for HEX-Editor and click install. After this you can change your file view to hexadecimal.

For Sublime Text there is also a plugin, which does the same.

Upvotes: 22

Rex U
Rex U

Reputation: 11

Remove Unwanted characters from $key == "transmission" or ? <=

? <=` This causes the problem

Upvotes: 0

Florian
Florian

Reputation: 2874

Copy of comment: One suggestion: Use in_array or something else to check a value of a variable against multiple values:

$values = array( 'listingURL', 'interiorColor');
if ( in_array( $key, $values) ) { //do stuff

Upvotes: 0

Meenesh Jain
Meenesh Jain

Reputation: 2528

try something like this

$arr = array("listingURL","interiorColor","engine","transmission","stockNumber","VIN");
if(in_array($key, $arr))
{
   // do something 
} 

this is much more effective way of doing things

Upvotes: 0

Rex Rex
Rex Rex

Reputation: 1030

When i copy in other editor i get this remove it near stock number ? <=

if ($key == "listingURL" or 
     $key == "interiorColor" or 
     $key == "engine" or 
     $key == "transmission" or 
     $key == "stockNumber" or ? <=
     $key == "VIN") {
          // Do thing
}

Upvotes: 2

Related Questions