171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

Hi, Hope u people will be fine. I’m working on a theme in which i want to use multiple php if conditions with multiple not operators (example is below), but when i execute following php code, it always returns true (mean content in always parenthesis always executed) even no condition is true. I want to ask what is the problem in following code. Is there any specific syntax or rule for using multiple != operator in php conditions. And i am amazed to see that if i use following code by replacing != operator with == operator it is working fine.


<?php if ($sidebar != "portfolio" || $sidebar != "right" || $sidebar != "left") { ?>

----- content which will be executed if above condition returns true -------

 <?php } ?>

Waiting for your kind replies. and sorry for my bad english :)

659 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
luckykind says

you need to replace your ORs ( || ) with ANDs ( && )

A couple of != with an OR ( || ) will always equal true because that string variable will only equal one thing and not be the other…

171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

Thanks for reply, but replacing all || with && will not cause to execute the stamement in parenthesis only if all conditions all TRUE ?

I want to execute the statement even if only one condition is TRUE .

171 posts
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
MsTrends says

@luckykind: you rock. i just tried your suggested solution, its working fine. Although i still can’t understand the logic. :)

by
by
by
by
by
by