Are empty curly brackets if the statements are satisfied?

This code works for what I want, but I'm wondering if it's okay to do anything inside the curly braces of the if statement. Is there a better way to write this?

if(empty($data) || $data == "unanswered")) {
//do nothing
} else {
    //display data
    echo $data;
}
+4
source share
6 answers

Instead, you can do the negation:

if(!empty($data) && $data != 'unanswered')
    echo $data;
+15
source

This is legal in PHP and most other languages, but you can use the Boolean DeMorgan Law to shorten the expression if:

if(!empty($data) && $data != "unanswered"){
    echo $data;
}

DeMorgan's law simply denies Boolean expression.

+2
source

. . DeMorgan ( ). , .

+1

. , . ! (). :

if( ! ( empty($data) || $data == "unanswered" ) )  {
    // do something
}
+1

, . .

0

- , . , , -, , .

, , .

0

Source: https://habr.com/ru/post/1650069/


All Articles