Output issue

I am having an issue outputting any of the html. The function outputs fine. I have tried echoing each line of html with no luck as well. What am i doing wrong. Thanks for any help.

<?php if (my_function() !== null) : ?>
	<div class="list">
		<p class="clickable">Click To Find</p>
	<ul class="collapse">
<?php my_function (); ?>
	</ul>
	</div>
<?php endif; ?>

My output should look like this

<div class="list">
    <p class="clickable">Click To Find</p>
	 <ul class="collapse">
            <li>my function</li>
	</ul>
</div>

First off, can you provide the output you are currently getting?

Hello,

Yes, I am just getting the

<li>my function</li>

which is generated by

<?php my_function (); ?>

Okay, my first recommendation is to replace

if (my_function() !== null) :

With

if (function_exists('my_function')) :

See if that helps resolve your output

Thanks for the recommendation, but in this case the function will exist, but it does not have any values stored in it. That is why I had to use the previous.

You can’t use the previous if you are performing an echo, print, or similar function in your my_function(). As the output will always occur regardless of what is returned.

Instead of performing output within your function, return what you want outputted.

Example:

function my_function()
{
  return '<li>my_function</li>';
}

That would change your code to the following:

<?php $myFunctionOutput = my_function();
      if ($myFunctionOutput !== null) : ?> 
    <div class="list"> 
        <p class="clickable">Click To Find</p> 
    <ul class="collapse"> 
<?php echo $myFunctionOutput; ?> 
    </ul> 
    </div> 
<?php endif; ?>

@cpradio thanks for working with me. I had previously assigned a variable to the function and that works well, but I am still not getting any of the html part returned. All I get is this

<li>the function</li>

This part is not being outputted at all

<div class="list"> 
        <p class="clickable">Click To Find</p> 
    <ul class="collapse">

I of course could add all of the above into the function so it outputs with the rest, but that would require me to use those styles, which will not work in this case. I do apologize if I am confusing, maybe returning it in the function is my only option.

Can you paste your function, and the code that calls your function (the updated versions) so I can see them. I must be assuming something that isn’t quite right.

Its a pretty big function to paste and I do not want for you to have to read through all the code. I do appreciate you offering though. I will try again to explain what it is I want to do.

Basically, I have a function that stores some values. If those values are true, it will output this

<div class="list">
    <p class="clickable">Click To Find</p>
	 <ul class="collapse">
            <li>my function output</li>
	</ul>
</div>

However, I have ran into one case that the values will be empty so nothing will output from the function. However, the html is still getting displayed .I dont want the html to display the value if the function is null.

So with the above code I can return the function if it is true or not. The issue is, that I need to output The <div class=“list”>, <p class=“clickable”>Click To Find</p> and <ul class=“collapse”>with it, without putting that html directly in the function. Hopefully that is clear as mud, if not, I will accept the title as bumbling idiot of the day :)…

I would have thought this code would have worked then:

<?php $myFunctionOutput = my_function();
      if ($myFunctionOutput !== null) : // did you see my change here? 
?>  
    <div class="list"> 
        <p class="clickable">Click To Find</p> 
    <ul class="collapse"> 
<?php echo $myFunctionOutput; ?> 
    </ul> 
    </div> 
<?php endif; ?>

Yes, I did see that and I changed it so its clean as you wrote it, but the <div>, <p> and <ul> are not outputting.

So the output being generated is null then? You would need to look into why that is. If you are still seeing the <li>my_function</li> output, that is because the data is not being returned, it is being outputted. To rectify that, you may have to use output buffering

Something like:

<?php 
  ob_start();
  my_function();
  $myFunctionOutput = ob_get_contents();
  ob_end_clean();

  if ($myFunctionOutput !== null) :
?>  
    <div class="list"> 
        <p class="clickable">Click To Find</p> 
    <ul class="collapse"> 
<?php echo $myFunctionOutput; ?> 
    </ul> 
    </div> 
<?php endif; ?>

Thank you cpradio. I appreciate all your help. I will give that a try and read up on it as well.