So for example, say I have a piece of text from a field I want to call.

So I set the variables like this:

$text = get_field('text_field');
$outputtext = echo $text; ?>

<p><?php $outputtext ?> </p>

Is that not possible, and if so, why? Is there a particular reason why PHP is set up that way or something?

  • That's just not how PHP's echo works. Just do <p><?php echo $text; ?> </p> instead. There various ways to set up the output string depending on your needs. Commented Jun 21, 2017 at 0:06
  • @DaveRomsey okay understood. Is there a reason that's not how PHP works though? Commented Jun 21, 2017 at 0:51
  • 2
    echo $text (can also be written as echo($text)) is a command that sends a string to the output stream (i.e. the response body for a web page or the screen on the command line) so trying to assign it's returned value (which is always nothing) to a variable simply makes no sense. If you want a more detailed explanation, it'd probably be best to ask over on Stack Overflow. Commented Jun 21, 2017 at 13:58

1 Answer 1


Not a WordPress question, but to do what you want you'd need to use an anonymous function.

For your example, it's totally not needed or recommended, though.

Echo shortcut syntax (e.g. <?= $text ?>) might interest you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.