How to put an image inside the echo and not show if it is empty

0

I made the code to show only if it has image, but it also shows the body of the empty, it says in the source code of the page that my PHP is a comment as shown in the image below.

Andthecodeisthis:

HowdoInotshowtheemptyphotosthewayyouarehere?

echo'<php$seleciona=mysqli_query($conexao,"SELECT * FROM postagem where status=1 ORDER BY id desc");
                  while($campo=mysqli_fetch_array($seleciona)){
                    $nome_imagem = $campo["nome_imagem"];                    
                    ?>
                    <center>
                    <div id="panel" align="left">
                    <label class="titulo">&nbsp;&nbsp;'. $result1[$i][1] . '</label><br>
                    <p class="descricao">'. $result1[$i][2] . '</p><br>

                    <?php if ($nome_imagem != null){?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

                    <span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;&nbsp;Postado por: '. $result1[$i][6] . ' em '. $result1[$i][4] . ' às '. $result1[$i][5] . '</td>&nbsp;&nbsp;

                    <a href="0_excluir_postagem.php?editaid='. $result1[$i][0] . '"><span class="glyphicon glyphicon-trash"></span></a>&nbsp;&nbsp;     
                    </div></center>  

                    <?php }?> 
                    </div>' ; 

                  }
    
asked by anonymous 18.12.2018 / 00:57

1 answer

0

Change:

<?php if ($nome_imagem != null){?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

To:

<?php if (!empty($nome_imagem)) { ?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

I did not understand the logic of testing one variable ( $nome_imagem ) and displaying another ( $result1[$i][3] ). But it's alright. It is not the purpose of the question, much less the answer.

Returning to the question, about empty() :

Return FALSE if the tested variable exists and is not empty. Otherwise, it returns TRUE .

empty() considers the following cases as empty :

  • "(an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a floating point)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array () (an empty array)
  • $ var; (a declared variable but no value)
  

Reference: PHP - empty

Comparing the variable with null would ignore all these other cases mentioned. (% with%).

    
18.12.2018 / 02:19