-1

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

So I set the variables like this:

<?php
$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. – Dave Romsey Jun 21 '17 at 0:06
  • @DaveRomsey okay understood. Is there a reason that's not how PHP works though? – David A. French Jun 21 '17 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. – Andrew Dinmore Jun 21 '17 at 13:58
0

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.

|improve this answer|||||

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