5

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?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
SoWizardly
  • 387
  • 1
  • 6
  • 16

5 Answers5

22

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

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    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 Apr 23 '15 at 13:26
  • 1
    @SoWizardly Well, I use Notepad++ and that came up as an Unicode. I have never used Sublime, but there's probably an viewing option in it. – Funk Forty Niner Apr 23 '15 at 13:27
  • Nice catch. EDIT: NVM, just saw you use a different text editor. That may just be the case :) – Albzi Apr 23 '15 at 13:27
  • 1
    @BeatAlex strangely enough, I started off with regular Notepad, noticed the little square at the end of the line, slapped into Notepad++ and also my HTML page editor to confirm. – Funk Forty Niner Apr 23 '15 at 13:29
  • wow, couldn't have checked this +1, does anyone know how to do this in sublime? i'd like to know it as well. anyway, great catch Fred. – Kevin Apr 23 '15 at 13:43
  • @Ghost Thanks Ghost. Good question though. I never used it so I can't say unless I researched it. – Funk Forty Niner Apr 23 '15 at 13:44
  • 1
    @SoWizardly I found this Q&A http://stackoverflow.com/q/20356784/ and https://packagecontrol.io/packages/Unicode%20Character%20Highlighter and http://stackoverflow.com/a/20999477/ - there were more links while Googling "how to detect unicode characters with sublime". I hope it helps. – Funk Forty Niner Apr 23 '15 at 13:49
  • @Ghost I made an edit to my answer under Sidenotes about plugins. I hope this helps. – Funk Forty Niner Apr 23 '15 at 14:00
  • @Fred-ii- yeah thanks for that, never would have checked for these kinds of error lol :D i'd just look at the question as it is. – Kevin Apr 23 '15 at 14:03
  • 2
    @Fred-ii- Thanks for all your help! I was having some issues getting a .csv file importer to work, and it looks like that character came from me copy+pasting...which lead me to realize that's what was causing the problem...I ended up finding a nice PHP script that strips these characters from strings, and now everything works as intended! – SoWizardly Apr 23 '15 at 14:15
  • @SoWizardly You're very much welcome. I'm glad you were able to find the root of the problem and to solve it as you said, with the script you found. *Cheers* – Funk Forty Niner Apr 23 '15 at 14:16
  • There is no hidden char in phpstorm or probably auto remove? – Hendra Nucleo Apr 05 '16 at 23:49
  • @Nucleo1985 That I couldn't say, I've never used phpstorm. – Funk Forty Niner Apr 05 '16 at 23:54
  • The same person who just downvoted this answer, also downvoted http://stackoverflow.com/a/18219669/1415724 and suspect a certain someone who couldn't take the fact that his answer was wrong. You have a lot of nerve. – Funk Forty Niner Jan 30 '17 at 17:18
2

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
}
Rex Rex
  • 1,030
  • 1
  • 8
  • 29
0

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

Meenesh Jain
  • 2,532
  • 2
  • 19
  • 29
0

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
Florian
  • 2,796
  • 1
  • 15
  • 25
0

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

? <=` This causes the problem
Rex U
  • 11
  • 7