Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

What is the difference between include() and calling a function in PHP? For example :

1-

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

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

like image 264
TheNone Avatar asked Aug 06 '26 03:08

TheNone


1 Answers

include() simply takes the full contents of the file and inserts it in, replacing the include() with the contents of the file.

If you have HTML in the included file, it will be output. If you only have PHP in it, the PHP will be run.

To call a function, the function must be available. If the function is in another file, you will still need to include() or require() that file to have it available.

like image 106
Alan Geleynse Avatar answered Aug 08 '26 16:08

Alan Geleynse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!