PHP: return value from function and echo it directly?

matt picture matt · Jun 13, 2012 · Viewed 87.2k times · Source

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!

Answer

Scott Saunders picture Scott Saunders · Jun 13, 2012

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;
}