PHP echo function return value vs echo inside function

php return type mixed
php function return type
php echo variable in html
php return multiple values
php return array
php function not returning value
php function default return value
how to store echo value in variable php

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 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"


Returning values - Manual, echo square(4); // outputs '16'. ?> A function can In strong mode, the returned value must be of the correct type, otherwise a TypeError will be thrown. As of PHP This signifies that the function returns either the specified type or NULL . Note:. Or use the PHP's shorthand for echoing: Though I recommend you keep the echo. It's more readable and easier to maintain returning functions, and the shorthands are not recommended for use. One visit to echo's Manual page would have yielded you the answer, which is indeed what the previous answers mention: the shortcut syntax.

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.

echo - Manual, echo is not actually a function (it is a language construct), so you are not one parameter to echo, the parameters must not be enclosed within parentheses. to print are that echo accepts an argument list and doesn't have a return value. just a guess, but it looks like STDOUT (ie what echo is printing to) and the return value are being dumped into the same stream. You might be able to force them to be separate by telling echo to print to STDERR: echo "afdsfadsf" 1>&2; – mlathe Jan 28 '10 at 23:32

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.

return - Manual, If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. echo "a"; ?> b.php <?php echo "b"; return; ?> (executing a.php:) will echo "ba". Tip: The echo() function is slightly faster than print(). Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.

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

Manual, Shutdown functions and object destructors will always be executed even if exit is called. If status is an integer, that value will be used as the exit status and not printed. echo 'This will not be output.'; Could not sound obvious: for instance in Java you never issue an exit, at least a return in your controller; in PHP instead​  echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo , the parameters must not be enclosed within parentheses.

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() {
    '<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>
add_shortcode( 'eddminicart', 'eddminicartfunc' );

PHP echo() Function, PHP echo() Function. ❮ PHP String The echo() function outputs one or more strings. Note: The echo() Return Value: No value is returned. PHP Version Write the value of the string variable ($str) to the output, including HTML tags: <?​php Original Value is 10 Original Value is 16 PHP Functions returning value. A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code. You can return more than one value from a function using return array(1,2,3,4).

PHP Functions, PHP has more than 1000 built-in functions, and in addition you can create In the example above, notice that we did not have to tell PHP which data type the function sum(int $x, int $y) { $z = $x + $y; return $z; } echo "5 + 10 = " . sum(5, 10) . Is there any way to return HTML in a PHP function? (without building the return value as a string) echo) the HTML inside the function. Is there any way to do this

When is the right time to use return vs echo?, PHP PHP Functions Function Returns and More Returning Values How do I know that I need to use return on this or use echo on this? Return is commonly used in a function to return the output of the function i.e.. /*Function to return a value*/ public function returnName($name){ /* See here i'm  Return type declarations. PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.

How to use return in my custom function instead of echo , If you use do_action( 'woo_collections_menu' ); in the template, then your function must echo its value. Otherwise, you are return ing the data into a black hole,  Functions in PHP. In this part of the PHP programming tutorial, we talk about functions. A function is a piece of code in a larger program. The function performs a specific task. The advantages of usin

  • can you explain why?