开发者

PHP echo function return value vs echo inside function

Typically, I'll write a function like so:

function alertClass($field,$full=false){
 global $formErrors;
 $html = $full ? ' class="alert"' : ' alert';
 if (!empty($formErrors[$field])) return $html;
}

and then where I want the html to show I'll echo the return value of the function like so:

echo alertClass('somefield')

but today I was thinking why not just put the echo in the function instead of using it's return value? So 开发者_Python百科instead of "return $html" it would be "echo $html"... Is there an advantage to one way or the other?


for example when you echo the text out of your function just like this...

function yourStatus(){
   echo ' Done';
}

echo 'Status ='. yourStatus();

your output will look like this

"DoneStatus ="

instead of

"Status = Done"

cheers


Using echo preculdes using the function to programmatically build some HTML for output later on, or for further processing.

If in your case there's no downside to returning the HTML, I'd continue doing it. It adds flexibility.


It depends on the purpose of the function.

In general, you will want to have your functions as side-effect free as possible. If you go about echoing output in several places, your code will start to get very confusing. A function that returns a value is also more versatile, since the caller can decide whether to further manipulate that value or immediately echo it.

However, if the purpose of the function is specifically to output text (e.g. methods on a class responsible for building and outputting a page, according to a template), then it would fine.


If you are not using the returned value again anywhere, then directly echoing is better.


echo is a PHP language construct which pushes values to the output buffer. It does not have a return value, so concatenating it with a string would cause everything after the echo to immediately be sent to the output buffer, and everything prior to echo to compose the concatenated string. This is such a misuse of echo that PHP itself doesn't actually allow it - if you had WordPress debugging enabled you would see an error similar to

Parse error: syntax error, unexpected 'echo' (T_ECHO)

This error is what is causing your white screen - when not in debug mode, WordPress suppresses error output to avoid exposing potentially sensitive information to end-users.

You shouldn't use echo in shortcode logic, as internally WordPress does more processing with a shortcode's return value. So using echo in a shortcode has a good chance to mess up your final markup.

The inclusion of the echo before the edd_get_cart_total() does not result in currency formatting. I've dug through the plugin in question's source code just to be sure. Rather, it's more likely that some function is hooked to the edd_get_cart_total filter to format the output in templates (thus formatting the total when you used it in your header.php template), however within the context of a shortcode that filter is not attached.

Conveniently, the plugin provides the ebb_cart_total() function which will always produce a currency-formatted total string. The first argument to the function is $echo which is true by default, and will cause the function to display the total instead of returning it - which, as detailed earlier, is not something you want to do in a shortcode - so set this argument to false to have the function return a string which you may concatenate with the rest of your shortcode markup.

All together:

function eddminicartfunc() {
  return 
    '<div class="mobilemenucart">
       <i class="fa fa-shopping-cart"></i>
       <span class="header-cart-total"> ' . edd_cart_total( false ) . ' </span>
       <span class="header-cart edd-cart-quantity">' . edd_get_cart_quantity() . '</span>
    </div>';
}
add_shortcode( 'eddminicart', 'eddminicartfunc' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜