php - Remove blank line from else statement

6

I've been researching if/else statements, and I've built one without any issue, on my else statement I use$tpl->set('message', ''); the '' quotes are to indicate to not show anything (which is what I want), on my template I use {message} to echo a message during the if statement, but I was wondering, as I don't want to echo anything on an else statement is leaving the quotes empty best practice?, as with it empty it keeps the {message} hidden on my template which is what I wanted, sorry if my question is rather basic, I'm still learning, thank you very much.

Edit: Just to clarify with the title, on the html output where the {message} would appear is a blank line, is it possible to remove it?

278

Answer

Solution:

Yes you can. You can just check if themessage is empty or not using theempty(). If the $message is not empty, only then show the message as below.

 <?php if(!empty($message): ?>
   {$message}
 <?php endif ?>

Note: I don't know what templating you are using. So, this is the basic idea, you might need some adjustment to fit it for the template you are using.

People are also looking for solutions to the problem: how to retrieve json response objects which stores values inside nested arrays in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.