Home > Software design >  How to remove all php function contents if return false?
How to remove all php function contents if return false?

Time:07-09

hey guys i have function in php and i am trying to find a way to remove all function contents if the result is empty or false here is an example:

function myfunction($result){
?>
 <div><a>Hello</a></div>
<?php
   if(empty($result)) {
      return false;
   }
}

my problem is when the return is false, the function output is

Hello

and i want to remove it if return is false.

CodePudding user response:

You have to return false before outputting anything

function myfunction($result){
    if(empty($result)) {
        return false;
    }
    ?>
    <div><a>Hello</a></div>
    <?php
}

CodePudding user response:

You can return empty/false response when it is empty and return the HTML when it is not, like below:

<?php

function myfunction($result){
  return empty($result) ? false : "<div><a>Hello</a></div>";
}

CodePudding user response:

<?php
function myfunction($result){
  if(empty($result)) {
      return '<div></div>';
   }else{
       return "<div><a>Hello</a></div>";
   }
}
echo myfunction($result);

   
  • Related