1

In JavaScript, I can go:

console.log( 'valueA' && 'valueB' ); // 'valueB'

But in PHP:

echo 'a' && 'b'; // 1

As you can see from the above example, the behavior is different. In JS, the value is printed, while in PHP, the boolean is echoed instead.

Is there a PHP operator that will behave like JavaScript's && for short-circuit evaluation? I need the value, not boolean, printed.

In PHP, there is the ?? null coalesce operator, but it doesn't check truthiness, it only checks for null:

echo 'valueA' ?? 'valueB'; // 'valueA'

echo null ?? 'valueB'; // 'valueB', only works as desired with null.

As you can see from the above example, ?? is similar to JavaScript's &&, but it only triggers on null.

Without using if() statements and ternary statements, is there an operator in PHP (doesn't matter which version) that is the equivalent of JavaScript's && for short circuit evaluation?

GTS Joe
  • 3,612
  • 12
  • 52
  • 94
  • 1
    tbh, that feels like a design flaw in JS, why would it output valueB when your console logging a condition? o.O – treyBake Jan 29 '20 at 16:43
  • It's extremely useful, actually. For example, if you wanted to set a default value inside a function. Instead of writing an if() statement or ternary operator, using this method is more succinct. – GTS Joe Jan 29 '20 at 16:47
  • 1
    @treyBake — It isn't a flaw. It's a [very common feature of language design](https://en.wikipedia.org/wiki/Short-circuit_evaluation). – Quentin Jan 29 '20 at 16:47
  • @Quentin so I see.. but I'd still prefer it to evaluate the condition result, but maybe I've been using PHP too long :p – treyBake Jan 29 '20 at 16:49
  • @GTSJoe you can do that by using [parameters](https://www.php.net/manual/en/functions.arguments.php) in php – treyBake Jan 29 '20 at 16:49
  • @treyBake That was just one example, it doesn't solve the problem for other situations. – GTS Joe Jan 29 '20 at 16:52
  • @GTSJoe such as? – treyBake Jan 29 '20 at 16:54
  • For writing more succinct code and avoiding the use of traditional if() and ternary statements. – GTS Joe Jan 29 '20 at 17:03
  • @GTSJoe in what scenario though? Edit: this is irrelevant to question haha would get moved to chat - probs best to delete after :) – treyBake Jan 29 '20 at 17:04
  • In ANY scenario. For example, when checking if a bunch of HTML form input values are set, if not, to assign a default value to them. This type of check would be really succinct, similar to ternary statements by even more so. – GTS Joe Jan 29 '20 at 17:10
  • @GTSJoe again, you can give function parameters default values: `function foo($value = ''){}` - then pass formdata to to the function and all's good - though, if statements aren't a bad thing, the more checks, the more "perfect" the data is (against your defined rules) – treyBake Jan 29 '20 at 17:11
  • This would be more succinct. Please understand, I'm not looking for a way to do it, I'm checking to see if this specific functionality is in PHP. I already know there's other ways to do it. – GTS Joe Jan 29 '20 at 17:20

0 Answers0