PHP Includes
PHP Includes allow you to include one PHP file or HTML file inside of another. So let's make a file in which we can store all of our functions. Let's call that file functions,php
. Then let's put a greeting function in there.
<?php
function greeting(){
return 'Welcome to Includes';
}
?>
Now, if we want to use this function everywhere, we just have to add the functions.php
file in all the other files using a function called include
like this:
<?php
include "functions.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>My First PHP App</titile>
</head>
<body>
<h1>Home Page</h1>
<p><?php echo greeting(); ?></p>
</body>
</html>
The include
function has to be at the top of our file, just like the session_start
function.