1

I'd like to know if there are any problems with doing this:

($this->debug)
    && print "We're debugging right now";

instead of this:

if ($this->debug)
    print "We're debugging right now";

To me, it's just a style thing. Does it do exactly the same thing?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81

2 Answers2

7

Please don't. This is horribly unreadable. And it starts innocently enough, soon other "clever" developers would come and invent more "clever" ways of using this && operator to avoid those "horrible" ifs.

Please... Don't.


Functionally, yes, they will work the same.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
-3

The shorter the code (up to a point!) the better.

You should tell your code what to do. You should not be asking your code, if you are allowed to tell it what to do. Your coding, like love, should be Unconditional!

Need more arguments?

&& is better:


Conditionals aren't always bad, but they can be (and frequently are) abused. source

Some clever developers built-in similar solution directly into PHP, @Madara Uchiha the null coalescing operator ?? i.e. was introduced to PHP.

Functionally they will produce the same result. But one (&&) says you are a quality programmer, the other (if) says you are doing it as a side hussle.

Stop being scared of your code.

Be proud of your code.

Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51