Reputation: 6186
I want to call a function in one PHP file from a second PHP file and also pass two parameters to that function. How can I do this?
I am very new to PHP. So please tell me, should I include the first PHP file into the second?
Please show me an example. You can provide some links if you want.
Upvotes: 172
Views: 431481
Reputation: 449
I'm glad you're asking.
If putting all code into the file is a solution to the problem, we do understand that the code only works if it is complete.
Therefore this complete form needs to be achieved, always, to get a working program (the code is complete).
Now, if we would always put all code into a single file, the file would be growing and growing. This can become cumbersome and very hard to maintain and would require us to always read a lot of code only to make a change that makes a difference to the program, like adding new functionality, remove a flaw or most importantly to make it beautiful (the code is finished).
Luckily, the form of the code can not only be organized in lines, but those lines also in files that can be distributed across the file system. This allows us to distribute our lines of code. But it has the implication that multiple files need to be put back together in the right order to make the code complete again, otherwise we could never finish the code.
And all our beautiful program would be ruined!
Please show me an example.
Taking a book as an example, executing a program is like reading a book: We open its cover and start reading from the first up to the last page. Then we have completely read the book. We can imagine each page as one file and thanks to the order of the pages arranged in the book, its binding, everything can still make sense and first of all is usefully organized. A fascinating book of hundreds of pages we can read in a night. We can even read it loud and the kids around love to listen until they fall asleep.
I've drawn a book here, you can see the binding on the left, and it also shows how the binding works, the pages need to be arranged in an arc, as otherwise the book is not easy to unfold and to turn one page over the other when opened:
*=== FRONT COVER ===
*+------------------
* +------------------
* +------------------
* +------------------
*+------------------
*=== BACK COVER ====
Now in programming we have the code organized in files (and only later in so called memory pages) and in web-programming we don't have a front or back cover, but a request send to the server which then hits our PHP script and the response send back to the browser (the HTML page). All our code lies between those two and PHP needs to read it from front to back.
http://example.org/page.php
The URL is the spine of the book, therefore we place a file in a public folder so that it is accessible:
/home/user/projects/my-website/public/page.php
^- private ^- public
If we then allow the web server to execute PHP scripts in the public folder, it can actually act as a real front cover: The book can be imaginary pulled out and be opened.
Now the binding. It all stands and falls with a good binding, let us not get loose pages scatter around the first time the book is in use.
<?php
// ---------------------------------------------
// Upon request
$location = $_GET['location'] ?? 'here';
$user = $_SERVER['PHP_AUTH_USER'] ?? 'an ordinary user like you';
// -------------------------------------------
// Give me all the codes I need
require __DIR__ . '/../code1.php';
require __DIR__ . '/../code2.php';
require __DIR__ . '/../code3.php';
// ---------------------------------------------
// Untangle
$begin = once_upon_a_time(
in_a_land_far_far_away(
from_here($location),
there_was($user),
),
);
// ----------------------------------------------
// The part we most often do not understand how
// we got there, but make beautiful in the end:
$story = new CreativeWriter($begin);
$chapters = array();
for ($i = 0; $i < 3; $i++) {
$chapters[] = $story->nextChapter();
}
$conclusion = evolve($chapter[0], $chapter[1])
+ finale($chapter[2])
;
// -------------------------------------------
// Show and tell
?>
<body>
<form action="test3.php" method="post">
<div class="mb-3">
<!-- ... -->
This example shows the require
statement, a control structure we can make use of to arrange multiple files of lines of code one after the other:
<?php
// -------------------------------------------
// Give me all the codes I need
require __DIR__ . '/../code1.php';
require __DIR__ . '/../code2.php';
require __DIR__ . '/../code3.php';
The lines of those files (file contents) you can imagine as they were in there, PHP will read (load) those lines and execute them there (if those files start with the <?php
sequence) or output them (if not with that sequence, which you don't want, therefore add <?php
as the first line of each code file).
This would be the following files (with their full or absolute pathnames):
------------------------------------------+-----------
private | public
------------------------------------------+-----------
/home/user/projects/my-website/public | /page.php
/home/user/projects/my-website/code1.php |
/home/user/projects/my-website/code2.php |
/home/user/projects/my-website/code3.php |
------------------------------------------+-----------
You can already see like with the cover of the book, in web-programming we have public facing sides and the pages are within and private. Otherwise the pages would scatter around and someone could read a page out of order, just a fragment, and as you can imagine that would ruin our beautiful story.
First we make the code complete, but then we have to make it beautiful, too.
End of chapter two. Let me know how the story evolves.
You can provide some links if you want.
include
(PHP/FI 2.0, June 1996¹)include_path
directive (PHP 3, June 1998)require
/ include_once
/ require_once
(PHP 4, May 2000)set_include_path()
(PHP 4.3², December 2002)spl_autoload_register
(PHP 5.1, November 2005)Upvotes: -1
Reputation: 11098
Yes require the first file into the second. That's all.
See an example below,
File1.php :
<?php
function first($int, $string){ //function parameters, two variables.
return $string; //returns the second argument passed into the function
}
Now Using require
(http://php.net/require) to require the File1.php
to make its content available for use in the second file:
File2.php :
<?php
require __DIR__ . '/File1.php';
echo first(1, "omg lol"); //returns omg lol;
Upvotes: 219
Reputation: 111
you can write the function in a separate file (say common-functions.php) and include it wherever needed.
function getEmployeeFullName($employeeId) {
// Write code to return full name based on $employeeId
}
You can include common-functions.php in another file as below.
include('common-functions.php');
echo 'Name of first employee is ' . getEmployeeFullName(1);
You can include any number of files to another file. But including comes with a little performance cost. Therefore include only the files which are really required.
Upvotes: 11
Reputation: 2676
files directory:
Project->
-functions.php
-main.php
functions.php
function sum(a,b){
return a+b;
}
function product(a,b){
return a*b;
}
main.php
require_once "functions.php";
echo "sum of two numbers ". sum(4,2);
echo "<br>"; // create break line
echo "product of two numbers ".product(2,3);
The Output Is :
sum of two numbers 6 product of two numbers 6
Note: don't write public before function. Public, private, these modifiers can only use when you create class.
Upvotes: 11
Reputation: 2898
file1.php
<?php
function func1($param1, $param2)
{
echo $param1 . ', ' . $param2;
}
file2.php
<?php
require_once('file1.php');
func1('Hello', 'world');
See manual
Upvotes: 50