1

Is there a shorter syntax for the code below:

if (!isset($foo)) {
    $foo = $bar;
}
$foo += $bar;

=== EDIT ===

This syntax is slightly shorter:

isset($foo) ? $foo += $bar : $foo = $bar;

But I'm still wondering if it could be written in an even shorter way.

DevonDahon
  • 7,460
  • 6
  • 69
  • 114
  • 4
    Why don't you know whether `$foo` is set or not? Initialise it to `0` at the top of the code block, done. – deceze May 31 '18 at 08:50
  • I'm using this syntax to create sub-arrays in a `foreach` statement and increment it. I can't see any reference to incrementation is the question mentioned, I doubt it's a duplicate, but I may have missed something. – DevonDahon May 31 '18 at 16:44
  • If you want proper advice for your specific situation, you’ll have to show more code and not just a context-less snippet. – deceze May 31 '18 at 17:03
  • function increment(&$subject, $path, $amount=1){ $keys = explode('.', $path); $ref = &$subject; $depth = count($keys); $i = 0; foreach($keys as $key){ $i++; if($i < $depth) {if(!isset($ref[$key])) $ref[$key] = [];} else {if(!isset($ref[$key])) $ref[$key] = 0;} $ref = &$ref[$key]; } $ref += $amount; unset($ref); } $subject = []; increment($subject, 'key1.key2', 5); – Dieter Gribnitz Feb 23 '22 at 08:25
  • replace explode('.', $path) with explode('.', (string)$path) in the function above to prevent type error – Dieter Gribnitz Feb 23 '22 at 08:53

3 Answers3

-2
$foo = (!isset($foo))?0:$foo+$bar;

Try this.

Jignesh Patel
  • 1,028
  • 6
  • 10
-2

You should never need to do this kind of check to begin with. Initialise your variables at the start of the program/function/block, and you rarely ever need isset:

$foo = 0;

// here be dragons

$foo += $bar;
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    The question wasn't "Should I do this" it was "How do I write an if statement shorter" – ThallerThanYall May 31 '18 at 08:59
  • And the answer to "how to avoid shooting my face" is "don't point the gun at your head in the first place". – deceze May 31 '18 at 09:00
  • 2
    Using your own analogy, your answer to that question would be "Don't own a gun". – ThallerThanYall May 31 '18 at 09:02
  • Also perfectly acceptable, yes. – deceze May 31 '18 at 09:03
  • Doesn't help someone if they already own the gun, and want to keep using it though, does it? – ThallerThanYall May 31 '18 at 09:07
  • 1
    And now we're just squabbling about the semantics of an analogy. The point is, sometimes you need to fix a different issue than the one you're specifically asking about. – deceze May 31 '18 at 09:09
  • 1
    Don't get me wrong, I agree with you. But you have no context for the question. There could be a very good reason for the way it's written. Or, you know, guy just wants to know how to use IF shorthand, and used a bad example. If you'd like to keep on this move it to chat, by all means. – ThallerThanYall May 31 '18 at 09:11
-3
$foo = (!isset(foo) ? 0 : null);
$foo += $bar;

Or something to that effect. Ternary Operators for more information.

ThallerThanYall
  • 197
  • 2
  • 14