1

Searching for documentation on this is nigh-futile as search engines seem to assume I'm asking about the && operand. What I found on PHP.net, touches on its use as an operand but I'm still unclear as to how it is working in a code snippet I ran across.

! empty( $foo )
    and $bar .= 'string';

I think I understand that to be something akin to:

if ( ! empty( $foo ) ){
    $bar .= 'string';
}

But it doesn't seem to be a bracket-less if conditional (it breaks if I remove the and).

I should say that I do – or thought I did – understand how && an and would work in the context of a comparison. It is the use of it on its own line (partially) and that the $bar .= 'string' is not a test condition that prompted the question.

Updated

What I understand from the discussion in the comments is as follows:

If ! empty( $foo ) were to be false, it would short-circuit the operation; anything beyond the and would not be evaluated.

The and is still simply fulfilling its role as a comparison operator(?). In this usage, the $bar .= 'string' side of the and is not another test condition… or is it? Is it evaluating whether $bar is truthy (which, in PHP, it seems to be whether $bar was declared/defined beforehand or not), then continuing on to concatenate string to it?

Ito Pizarro
  • 1,607
  • 1
  • 11
  • 21
  • 2
    Just like `&&` in an IF; the right hand expression won't be evaluated if the left hand expression is false. – AbraCadaver Mar 17 '17 at 15:44
  • 1
    This: http://stackoverflow.com/questions/2803321/and-vs-as-operator – CD001 Mar 17 '17 at 15:44
  • Possible duplicate of ['AND' vs '&&' as operator](http://stackoverflow.com/questions/2803321/and-vs-as-operator) – Kuldeep Singh Mar 17 '17 at 15:46
  • Your first example is not a ternary. It has to be `some_variable = some_statement ? statement_is_true : statement_is_false;` – M. Eriksson Mar 17 '17 at 15:46
  • http://php.net/manual/en/language.operators.logical.php – u_mulder Mar 17 '17 at 15:46
  • [`$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];`](http://php.net/manual/en/language.operators.comparison.php) that's a ternary. – Funk Forty Niner Mar 17 '17 at 15:47
  • `if/else` is a _Control Structure_ and `and` is a _Logical Operator_. Dont compare them in that way. – JustOnUnderMillions Mar 17 '17 at 15:47
  • 1
    Depending on your point of view it's an idiomatic shorthand, or an abominable misuse of the operator over an `if`. – deceze Mar 17 '17 at 15:48
  • @Ryan Vincent `will evaluate both operands` Really? `($false=false) and ($true=true);var_dump([$false,$true]);` or did i not understand it? – JustOnUnderMillions Mar 17 '17 at 15:53
  • @Ryan Vincent I use this oftern for something like `DEBUG AND print 'Debugnote';` first is defined constant with true or false. Nice onliner that can simple removed or commented. – JustOnUnderMillions Mar 17 '17 at 15:55
  • @MagnusEriksson (and Fred -ii-) Yep. "ternary" was not the term I should have used. Corrected. I think. – Ito Pizarro Mar 17 '17 at 15:58
  • 1
    @JustOnUnderMillions Why not `if (DEBUG) print 'Debugnote';`…?! – deceze Mar 17 '17 at 16:01
  • @deceze Someone will scream: The coding guide says that you should always you `{}` when using `if` ;--) And the `{` comes into the next line :-) And i can easy just remove or coment out lines that begin with `DEBUG AND` 8-) – JustOnUnderMillions Mar 17 '17 at 16:12
  • 1
    @JustOnUnderMillions Quite honestly I'll scream even louder when `and` is abused this way; the real proper way to do this is something like `log(DEBUG, 'Debugnote')`. – deceze Mar 17 '17 at 16:16
  • @deceze php itself ist most of the time abused. :-) Im above screaming for a long time now – JustOnUnderMillions Mar 17 '17 at 16:19

1 Answers1

2

That's short-circuit evaluation. It can be understood more easily if you break it down:

!empty($foo)            // 1) expression
   and                  // 2) logical operator
      $bar .= 'string'; // 3) assignment, expression
  1. empty($foo) will be evaluated, returning true or false, which will then be negated
  2. and could as well be &&, it is a logical operator (the difference between the two is their precedence as explained in this answer)
  3. $bar .= 'string' is an assignment and in PHP those are expressions, too. They evaluate to the assigned value, in this case some string. As long as that string isn't empty or "0", it will evaluate to true.

Only if !empty($foo) yields true does PHP ever look at what comes after and:

! empty( $foo ) // If false, stop right here, otherwise...
    and $bar .= 'string'; // ...see what this evaluates to

Since in your example, the result of the whole expression is not used in any way, it does not matter what the string assignment evaluates to - the trick is simply to only execute it depending on !empty($foo). Therefore, your if version indeed has the very same effect.

Now, why does this break when you remove and? Because then you end up with invalid PHP: !empty($foo) $bar .= 'string';

Slightly changing the original snippet, using && instead of and and adding some brackets, make it a lot easier to read and understand:

!empty($foo) && ($bar .= 'string');

Also, this kind of shortcut is very common when using PHP with mysql. You have probably seen this line at one point or another:

mysqli_connect($host,$user,$pass,$db) or die();

It is the same logic. If the connection can be established, mysqli_connect() returns an object (which evaluates to true) and the second part will never be looked at. If the connection fails, however, die() will be evaluated and script execution ends.

Community
  • 1
  • 1
domsson
  • 4,553
  • 2
  • 22
  • 40
  • 1
    Much appreciated, thank you! And now, when a n00b like me Googles "what is and in PHP?" they, hopefully, will see your answer. – Ito Pizarro Mar 18 '17 at 02:14