What is the difference between include () and function call in PHP?

What is the difference between include () and function call in PHP? For instance:

1 -

<?php
   $foo = '<p>bar</p>';
   return $foo;
 ?>
<html><body><?php echo $foo; ?></body></html>

2-insert above php code into php file and include () in advance thanks

+3
source share
3 answers

Typically, inclusion is used to get a set of functions or objects in your script run so that they can be used, although it can also be used as a separate page or some bit of HTML, as you wrote. In reality, it depends on whether you want to have another function in the same script or remote script, for aesthetics or organization, regardless of your reason.

, include , . , , include , . .

+2

include() , include() .

HTML , . PHP, PHP .

. , include() require(), .

+6

As a complement to existing answers, you can also do this:

sample.php:

<?php
$foo = include('include_with_return_value.php');
?>

<html><body><?php echo $foo; ?></body></html>

and include_with_return_value.php:

<?php
return '<p>bar</p>';

So files include()can also have a return value, like functions.

+3
source

Source: https://habr.com/ru/post/1771782/


All Articles