Jump to content

PHP very weird issue (to me) in IF statements


Recommended Posts

Posted (edited)

Hi there,

Here's my problem which lets me... with a big ???? in the head :)

I have this simple piece of code :

if ($page->name === 'blog' OR $page->name === 'documentation') {
 $class = 'on';
} else {
 $class = '';
}

It triggers the error :

Parse Error: syntax error, unexpected '$page' (T_VARIABLE) (line 114 of /home/celfred/PlanetAlert/site/templates/head.inc)
This error message was shown because you are logged in as a Superuser. Error has been logged. 

If I get rid of the OR statement, it works fine.

If I choose the right part of the OR statement alone, it works fine.

If I type || instead of OR (what I did at first), it throws the error.

And here's the best part : If I copy/paste the || from another IF statement that works fine in my site, it WORKS !!

This is becoming a nightmare. I can't type a regular OR statement in my PHP code. I have to copy/paste the characters and it works.

If anyone has a clue to help me on that, I would really appreciate because as I often say, being no professional coder, I kind of take A LOT of time on programming issues, but here, this weird behavior is over my head. I feel desperate when I stumble upon such basic parts of code :(

Thanks in advance !

PS : I've tried to dig into my PHP config that would have changed since some kind of upgrade, but with no luck... I've looked at my file encoding (utf8)... Well, I'm stuck !

EDIT : Just to illustrate more : Copy/paste from another file that works :

if ($page->name == 'blog' || $page->name == 'documentation') {

If I retype the || operator and save and reload on my localhost : tada! Error !!!

Edited by celfred
Posted

Can you post the full example of the code? The error could be from code above the snippet you posted - i.e. a missing semicolon -;- or closing bracket -}- or something.

Alternatively you could try:

$class = $page->is("name=blog|documentation") ? "on" : "";
  • Like 4
Posted

Hi Celfred!

Maybe you're having non-breaking spaces in your if statement, which causes the error. Try clearing spaces in your statement and rewrite them. At least on mac version of Sublime Text alt+space inserts non-breaking spaces. 

If you're using Sublime Text, add this line to key bindings configuration (Settings > Key bindings - User) to prevent those nasty spaces:

{ "keys": ["alt+space"], "command": "insert", "args": {"characters": " "}},
  • Like 3
Posted (edited)

@arjen : Your code works fine. Thanks! I like the syntax very much and try and use it in the future.

My entire code would be a little long to put here, I think and I'm not sure it would help. I can't imagine a ';' or a '}' is missing because everything worked fine before. My previous line was :

$class = $page->name == "blog" ? "on" : "";

And i just tried :

$class = $page->name == "blog" || $page->name == 'documentation' ? "on" : "";

and

$class = ($page->name == "blog" || $page->name == 'documentation') ? "on" : "";

And in both cases I had the error. Then, I tried the longer version I mentionned a tfirst, and still the error occured.

And I had to face the same issue (twice actually) a couple of days ago in another part of my code. I eventually found the workaround of copying the || from another place where it works, and as I said, just this manipulation of copying and pasting ' || ' makes the code work as expected. Which is why I wondered if there could have been an issue of character encoding that I would be absolutely unaware of, and this is why I decided on posting here :)

And I'm again glad I did since you're teaching me a nice concise syntax that I should use (and that avoids my problem). So thanks a lot!

But if you insist on seeing my code to try and understand the issue, I'll do it. If you have other ideas of where I should look from the info I've just added, tell me. I'd be happy to really understand this problem.

@Fokke : Thanks for your quick comment (hence my editing of this post ;) ) : I'm using VIM to code. And guess what : I've just tested your idea : getting rid of my spaces and re-typing them and....... YES! It works ! That's so cool! Now I'll try and understand why is my VIM acting this way and find a solution, but that's another matter.

Anyway, a big THANKS to both of you !

This place is really one of the best place on the Internet, and I mean it. I have learned so much here and the community is so 'responsive' when you need help. That's really cool :)

Edited by celfred
  • Like 2
Posted

Good to hear celfred. 

This place is really one of the best place on the Internet, and I mean it. I have learned so much here and the community is so 'responsive' when you need help.

We should pin messages like these on the door  :)

  • Like 3
Posted

Just as a follow-up : I've noticed this happened just because of my 'Alt-Gr' keyboard latency. I explain : when I type ' || ' on my keyboard, I need the 'Alt-Gr' key and the second space character is then often hit with 'Alt-Gr' down making it a non-breaking space :) And VIM didn't show any difference. (but PHP did!)

To finish : here's what I found that helped (in case it helps others) : https://wincent.com/blog/making-vim-highlight-suspicious-characters

  • Like 2
Posted

I see differences in the pieces of code:

  • Identity/Strict equality (===) / Equality (==)
  • Simple quotes / double quotes
  • One character space (at the beginning of a new line) / a tabulation (or more space)
  • Double quotes for blog / Simple quotes for documentation

Perhaps one or several of these could cause the problem, depending on where/how your code is inserted.

×
×
  • Create New...