4

Suppose I write a line

include Yii::app()->basepath.'/views/email/email_friend.php';

now how can i take the response of this line into a variable?

like

$abc = include Yii::app()->basepath.'/views/email/email_friend.php';
abhit
  • 973
  • 3
  • 17
  • 40

4 Answers4

5

Have a look at the PHP docs for include http://php.net/manual/en/function.include.php

Example #5 is I think what you're looking for

return.php
<?php

$var = 'PHP';

return $var;

?>

noreturn.php
<?php

$var = 'PHP';

?>

testreturns.php
<?php

$foo = include 'return.php';

echo $foo; // prints 'PHP'

$bar = include 'noreturn.php';

echo $bar; // prints 1

?>
Andrew Phillips
  • 324
  • 2
  • 8
1

All you have to do is the included file had a return with the desired value. It's been quite popular for some time.

so the include.php should like the following:

<?php
return ' World!';

and the including one:

<?php
$a = include('include.php');

echo 'Hello'.$a; // Hello World!
jakub wrona
  • 2,212
  • 17
  • 17
0

When you include it's like you're copy/pasting the code into your PHP. If it's just inline PHP and there was a variable $abc in the include file 'email_friend.php' then you could access the variable normally after the include.

sdexp
  • 756
  • 4
  • 18
  • 36
0

I know this is an old post. I hope my answer will be useful to someone. I combined the Accepted answer with the answer "PHP/7 you can use a self-invoking anonymous function..."

    define( 'WPPATH', dirname(dirname(__FILE__)) . '/public/partials/bla-bla.php' );
    $publicDisplayContent = (function () {
      // [PHP/7 you can use a self-invoking anonymous function](https://stackoverflow.com/a/41568962/601770)
      // https://stackoverflow.com/a/5948404/601770
      ob_start();
      require_once(WPPATH);
      return ob_get_clean();  
    })(); // PHP/7 you can use a self-invoking anonymous function
    error_log( 'activate() >> $publicDisplayContent: ' . print_r( $publicDisplayContent, true ) ); 

       DOT DOT DOT

    'post_content'  => $publicDisplayContent,