Can you call a function inside an echo statement in PHP?

1 answer

Answer

1136307

2026-04-22 16:05

+ Follow

Of course it is possible to call a PHP-function inside an echo statement. The function will be executed and returns a value. This value then is used in the echo statement.

For example:

echo "Ferengi-Rule #1: ", ferengi_rule(1), "\n";

echo "Random: ", ferengi_rule(0), "\n";

function ferengi_rule($number)

{

$rules = array(

1 => "Once you have their money, never give it back.",

2 => "You can't cheat an honest customer, but it never hurts to try.",

3 => "Never buy anything for more than is absolutely necessary.",

4 => "Sex and profit are the two things that never last long enough."

// ...

);

if( isset($rules[$number]) )

{

return $rules[$number];

}

else

{

return array_rand($rules);

}

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.