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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Modularizing Logic: Calling Functions Across Files in PHP

In PHP, treating logic as isolated units rather than monolithic scripts is a fundamental shift that scales your application's maintainability. If you have a function defined in one file and need to execute it from another, the core mechanism relies on the include or require statements. These directives load external files into the current script's execution context at runtime. When you include a file, the code inside it runs immediately, allowing any functions defined within that scope to become available to the calling script.

Consider a scenario where calculator.php defines a function named addNumbers. You cannot simply call addNumbers() directly from index.php without first ensuring the definition exists in memory. The standard approach is to include the file containing the definition before attempting to invoke the function.

Here is a concrete example of how this looks in practice:

calculator.php

<?php
/**
 * Adds two numbers together.
 * 
 * @param int $a First operand
 * @param int $b Second operand
 * @return int Sum of operands
 */
function addNumbers(int $a, int $b): int {
    return $a + $b;
}

index.php

<?php
// Step 1: Load the file containing the function definition
include 'calculator.php';

// Step 2: Call the function and pass two parameters
$result = addNumbers(5, 10);

echo "The result is: " . $result; // Outputs: The result is: 15

In this setup, index.php imports the logic from calculator.php. Once included, PHP treats addNumbers as if it were defined in index.php itself. You can then pass any number of parameters to the function, including the two required integers in this example.

The Distinction Between Include and Require

While both statements achieve code inclusion, they behave differently regarding error handling. Using include is generally safer when the file contains optional logic or utility functions. If calculator.php is missing, execution continues, though the function will be undefined if you try to call it. Conversely, require stops execution immediately if the file cannot be found, throwing a fatal error.

For utility libraries or shared functions where availability isn't guaranteed but desired, include offers flexibility. However, for core logic that the application cannot function without, require is often preferred to fail fast and alert developers to missing dependencies early in the debugging process.

Autoloading: A Modern Alternative

As your codebase grows, manually including files before every use becomes verbose and error-prone. This is where autoloading comes into play. Instead of writing include 'calculator.php'; at the top of every script, you register an autoloader that dynamically loads classes or functions when they are first referenced.

While PHP does not natively support function autoloading in the same way it handles classes, frameworks like Laravel provide robust mechanisms to manage dependencies and include necessary files automatically via Composer. This pattern decouples your entry point from your utility logic, allowing you to organize code into distinct directories based on purpose rather than a flat list of includes.

When to Use External Libraries

If your goal extends beyond simple file inclusion to integrating complex mathematical or data processing capabilities, consider using established PHP libraries. For instance, if you need advanced statistical functions that go beyond basic arithmetic, the LaravelCompany platform often recommends leveraging existing packages via Composer rather than maintaining custom utility files.

Using a dedicated library ensures your implementation adheres to community standards, receives regular security updates, and avoids reinventing the wheel. However, for simple logic like adding two numbers or formatting strings, keeping the code within your own modular PHP files remains the most lightweight and transparent solution.

Conclusion on Modular Design

The ability to separate logic into distinct files and call functions across them is a cornerstone of clean PHP architecture. By using include or require, you establish clear boundaries between different concerns while maintaining a unified execution context. Whether you stick with manual inclusion for small projects or adopt an autoloading strategy for larger applications, the underlying principle remains the same: define your logic once, and call it wherever needed by passing the necessary parameters directly to the function signature.

Our network