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

<?php # 1. Classic # 2. Weird # 3. With Breaks # 4. Short // classic if(!empty($var)) { // not empty } else { // empty } // weird if(!empty($var)): // not empty else: // empty endif; // with breaks if(!empty($var)): ?> <!-- not empty --> <? else: ?> <!-- empty --> <? endif; ?> <?php // short echo !empty($var) ? $var : 'default'; // is 5 greater than 1? echo 5 > 1 ? 'yes' : 'no'; # returns no because of math // is apple a banana? echo $apple == $banana ? 'yes' : 'no'; # returns 'yes' because in this case both are undefined echo 'apple' == 'banana' ? 'yes' : 'no'; # returns 'no' because apples are no bananas
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.