Call a function from another PHP file
| Introduction | |
| Create an array | |
| Related articles |
Introduction
It is often useful to split site code into several files in advance, to avoid working with a «bedsheet» tens of thousands of lines long.
Example
Suppose you have a file index.php and a file utils.php
In the file
utils.php
There is a make_head function to call from a file
index.php
passing the lang and title arguments
Example make_head function
<?php
// This is utils.php
function make_head($lang, $title) {
echo'
<!DOCTYPE html>
'
echo'
<html lang="'.$lang.'">
<head>
<title>'.$title.'</title>
</head>
}
?>
Calling from a file
<?php
// This is index.php
// Assign values to variables
$lang = 'ru'
$title = 'URN.SU'
// Include utils.php
require_once('utils.php');
// Call the function with two arguments
make_head($lang, $title);
?>
Note that I am not saying this is how you should do it. Just showing that it is possible to do it this way.
Article author: Andrei Olegovich