Hello friends !!! help me figure out once and for all with this record, I am still very confused, thank you in advance.

What does the record if($var) { echo "Hello"; } if($var) { echo "Hello"; }

Or write if(!empty($var)) { echo "Hello"; } if(!empty($var)) { echo "Hello"; }

I understand for example the record

 if(!empty($var) == TRUE) { echo "Hello"; } else { echo "Bye bye"; } 

explain the difference and how it works? And where in the short record else use? Thank!

    4 answers 4

    By default, the truth is checked in this kind of condition. Those. the if(!empty($var)) entry is identical to if(!empty($var) == TRUE) in meaning. Those. if(!empty($var)) reads "If the variable is not empty, then".

      one.

       if($var) { echo "Hello"; } 

      means that if the value of $ var can be set to TRUE , then the echo "Hello" command will be executed. See the last column of the table:

      enter image description here

      2

       if(!empty($var)) { echo "Hello"; } 

      This means that if the value of the $ var variable is not empty , the echo "Hello" command will be executed.

      More information in the type comparison table in PHP. Note the flexible comparison and hard comparison.

      3

      And where in the short record else to use?

       if($var) { echo "Hello"; } else { echo "Bye-Bye!"; } 

      although better like this ( ternary operator ):

       echo ($var ? "Hello" : "Bye-Bye!"); 

        An expression like $var == true , you see it as a required part of if() . But it is not. Understand the following: == this is the same action as addition or multiplication:

         $var = 5 + 3; //var = 8 $var = 5 * 3; //var = 15 $var = 5 == 3; //var = FALSE $var = 5 > 3; //var = TRUE $var = TRUE or FALSE; //var = TRUE 

        As a matter of fact, PHP simply executes your expression, without worrying about the operators, addition, multiplication, or comparison.

        After all logical and arithmetic operations have been performed, the result of this calculation, TRUE or FALSE , goes to if() .

          Record

           if($var) { echo "Hello"; } 

          similar to

            if($var == true) { echo "Hello"; } 

          Record

           if(!empty($var)) { echo "Hello"; } 

          similar to

           if(!empty($var)==true) { echo "Hello"; } 

          or

           if(empty($var)==false) { echo "Hello"; } 

          In the "short" record, set the same as in the "long":

           if(!empty($var)){ echo "Hello"; } else{ echo "Bye bye"; } 
          • Thank you very much !!! I completely figured out, I really got confused with this for a year, and finally got unraveled))) - Dmitriy