Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: return value from function and echo it directly?

Tags:

function

php

echo

this might be a stupid question but …

php

function get_info() {
    $something = "test";
    return $something;
}

html

<div class="test"><?php echo get_info(); ?></div>

Is there a way to make the function automatically "echo" or "print" the returned statement? Like I wanna do this … 

<div class="test"><?php get_info(); ?></div>

… without the "echo" in it?

Any ideas on that? Thank you in advance!

like image 308
matt Avatar asked Jun 13 '12 17:06

matt


People also ask

Can you echo a function in PHP?

Definition and Usage The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

What is return value of echo in PHP?

They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions.

How do you return a value within a function PHP?

Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called.

What is the difference between PHP echo and PHP return?

Echo is for display, while return is used to store a value, which may or may not be used for display or other use.


1 Answers

You can use the special tags:

<?= get_info(); ?>

Or, of course, you can have your function echo the value:

function get_info() {
    $something = "test";
    echo $something;
}
like image 165
Scott Saunders Avatar answered Oct 19 '22 12:10

Scott Saunders