How to call function of one php file from another php file and pass parameters to it?

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.

41.6k 11 11 gold badges 67 67 silver badges 83 83 bronze badges asked Nov 12, 2011 at 14:19 Pushpendra Kuntal Pushpendra Kuntal 6,176 20 20 gold badges 70 70 silver badges 119 119 bronze badges

5 Answers 5

Yes require the first file into the second. That's all.

See an example below,

Now Using require (http://php.net/require) to require the File1.php to make its content available for use in the second file:

196k 55 55 gold badges 444 444 silver badges 846 846 bronze badges answered Nov 12, 2011 at 14:25 11.1k 7 7 gold badges 42 42 silver badges 58 58 bronze badges

To prevent unauthorized access to the file content with all your functions in it, bury the functions (File1.php) above the DOCUMENT_ROOT and change its permissions to 'rwxr-x--x'.

Commented Apr 11, 2016 at 18:46

Unauthorized access to the file content should not be a problem since PHP is a server-side language, and so its contents will never get the the browser. See stackoverflow.com/questions/4299571/…

Commented Aug 11, 2022 at 20:03 81 4 4 bronze badges answered Nov 12, 2011 at 14:29 Dmitry Teplyakov Dmitry Teplyakov 2,898 5 5 gold badges 27 27 silver badges 46 46 bronze badges
function sum(a,b) < return a+b; >function product(a,b)
require_once "functions.php"; echo "sum of two numbers ". sum(4,2); echo "
"; // create break line echo "product of two numbers ".product(2,3);

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.

502 2 2 gold badges 5 5 silver badges 12 12 bronze badges answered Aug 8, 2015 at 7:47 Hafiz Shehbaz Ali Hafiz Shehbaz Ali 2,656 27 27 silver badges 21 21 bronze badges I've split my functions into separate files to include where needed. Commented May 10, 2016 at 20:17

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.

answered Aug 9, 2018 at 16:29 Abubkr Butt Abubkr Butt 111 1 1 silver badge 3 3 bronze badges

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!

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.

nextChapter(); > $conclusion = evolve($chapter[0], $chapter[1]) + finale($chapter[2]) ; // ------------------------------------------- // Show and tell ?>

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:

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

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.