Frank
Frank

Reputation: 1414

How to echo an function

I've been struggling to echo the output of a function. I tried this:

echo 'myFunction('foo')'; 

.. which obviously won't work, due to the extra single quotes. Any suggestions?

Upvotes: 0

Views: 163

Answers (3)

anon
anon

Reputation:

Try this:

echo myFunction('foo'); 

Upvotes: 0

Dany Caissy
Dany Caissy

Reputation: 3206

Let's take this function :

function getStr()
{
    return "hello";
}

It will simply return a string, which means, calling this :

echo getStr();

Has the same exact result as calling this :

echo "hello";

Which means, the result of your function can be treated just like a variable (except you cant modify it), so you can do whatever you want with the result :

$string = getStr() . ' - ' . getStr();
echo $string; // Will print "hello - hello";

Upvotes: 3

Frank
Frank

Reputation: 1414

After trying a little while, I tried calling echo as an function:

echo (myFunction('foo'));

This works perfectly. I couldn't find this elsewhere on the internet (maybe I'm just a bad googler). Anyways, I thought I could might share this with you guys. In case anyone ever runs into the same problem.

Upvotes: 0

Related Questions