-3

I found both 'echo' and 'return' are working fine to display in shortcode function.

function display_shortcode_content($atts) {
    echo "COMES"; 
}

function display_shortcode_content($atts) {
    return "COMES"; 
}

My doubt is what is difference between echo and retutn in the function?

Aftab H.
  • 1,517
  • 4
  • 13
  • 25

2 Answers2

0

echo used to get the final result of a function to get output.

return used to returns the value from a function.

 <?php

       function bar() { return 'bar'; } 

       $baz = bar(); 

       echo $baz;

 ?>
Nikhil
  • 312
  • 2
  • 13
0

Best practice is to use return. For example if you have something like this:

<div>
    <h1>Random Title</h1>
    [your_shortcode]
</div>

and in your shortcode function, if you echo some markup, the markup will appear before the div and h1, where as if you use return, it will come out at the position of the shortcode, where it is supposed to be.

William Gu
  • 26
  • 2