5 PHP short If / Else (PHP 7.x: kurze If / Else - Abfrage)

# 1. Classic # 2. Weird # 3. With Breaks # 4. Short # 5. Even Shorter 1.) Classic =========== <?php if($foo) { // not empty } else { // empty } ?> 2.) Weird ========= <?php if($foo): // not empty else: // empty endif; ?> 3.) With Breaks =============== <?php if($foo): ?> <!-- not empty --> <? else: ?> <!-- empty --> <? endif; ?> 4.) Short ========= <?php echo $foo ? $foo : 'fallback'; // is $foo empty echo !empty($foo) ? $foo : 'empty' // is 5 greater than 1? echo 5 > 1 ? 'yes' : 'no'; // no // Does 'a' equal 'b'? echo 'a' == 'b' ? 'equals' : 'no'; // no 5.) Even Shorter ("Elvis operator") <?php echo $foo ?: 'fallback'; // $foo or 'fallback' ?>
5 kurze If / Else (Wenn / Dann) Schreibweise in PHP (inkl. Elvis operator)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.