lisovaccaro
lisovaccaro

Reputation: 33956

Is it possible to run PHP include() inside an echo?

Is this possible to include an external file inside an echo?

This is what I am trying:

    echo 'stuff'.(include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php')).'morestuff';

I could simply write 3 lines but I wanted to know for simplification purposes.

Upvotes: 0

Views: 3433

Answers (6)

user5340092
user5340092

Reputation:

There's no need to do this. Just break the echo. Example below...

<?php
echo "
  $value1 <div id='1'></div>
  $value2 <div id='2'></div>
  // I want my include here
  $value3 <div id='3'></div>
";
?>

Change above to;

<?php
echo "
  $value1 <div id='1'></div>
  $value2 <div id='2'></div>
";
  include 'some_file.php';
echo "
  $value3 <div id='3'></div>
";
?>

Upvotes: 0

Lou Morda
Lou Morda

Reputation: 5165

I tested this and it works... I only put HTML code in the 'button.php' file...

echo 'stuff'; include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php'); echo 'morestuff';

Upvotes: 2

Barmar
Barmar

Reputation: 780869

It would probably be cleaner for the include file to define a function that returns the value you want. Then you would do:

include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php');
echo echo 'stuff'.button_func().'morestuff';

Upvotes: 0

xelber
xelber

Reputation: 4637

You certainly can. If the included file is returning anything, that will be printed. Includes basically evaluate the file and can be used as a normal function.

Upvotes: 0

SomeKittens
SomeKittens

Reputation: 39522

No. echo implicity calls __toString() on whatever is contained within the statement to be echo'd.

What you should do is include the file and have the HTML in the included file. No need to echo.

Upvotes: 0

xdazz
xdazz

Reputation: 160833

You should just use include.

<div>
<?php include $_SERVER["DOCUMENT_ROOT"].'/theme/snippets/follow-button.php'; ?>
</div>

Upvotes: 3

Related Questions