How would I call a function in another file?

How would I call a function in another file?

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

Stefan Bogdanescu

Founder & Senior Architect

How would I call a function in another file?

You have a file named error-status.php. Inside it, you've defined a function called validateHostName. The logic attempts to reject special characters, ensure the input isn't purely digits, and verify the string matches a specific alphanumeric pattern starting and ending with a non-hyphen character. Now you need to invoke this logic from a different file, say index.php, using a require_once statement.

The mechanism is straightforward, but there are nuances in how PHP handles namespaces and global scope that often trip up developers when moving beyond simple scripts.

The Direct Approach

In your main script, index.php, you simply include the helper file at the top of the document:

<?php
require_once('error-status.php');

$host = "example.com";
if (validateHostName($host)) {
    echo "Valid host.";
} else {
    echo "Invalid host.";
}
?>

When you execute require_once('error-status.php'), PHP executes the code inside that file immediately. Since validateHostName is declared in the global namespace without a preceding namespace declaration, it becomes a global function. Once the file is loaded, the function exists in the current execution context, and you can call it directly by name.

The Namespace Trap

If you look closely at the source code provided for error-status.php, you will notice the use of eregi and ereg. These are legacy functions deprecated since PHP 7.0. While they still function in many environments, relying on them introduces technical debt and potential compatibility issues with modern tooling or stricter error reporting levels.

Furthermore, consider the implications if you decide to refactor your project into a namespaced architecture later. If error-status.php were wrapped in a namespace like App\Utilities, calling it directly from the global scope would fail unless you explicitly referenced the namespace:

use App\Utilities\validateHostName; // Incorrect syntax for functions
// or
\App\Utilities\validateHostName($host);

Because your current function is global, you avoid this complexity today. However, mixing global functions with namespaced classes can lead to collisions or confusion regarding where a function actually lives. If you find yourself working on larger applications, moving such utility logic into a dedicated namespace is often the cleaner long-term choice.

Security and Logic Review

Before relying on validateHostName, it is worth scrutinizing the regex logic and character class used in the source material. The condition (strpbrk($hostName,'~!@#$^&*()=+.[ ]{}\|;:'",&<>/?')==FALSE)` checks for forbidden characters. However, note that this specific implementation uses single quotes inside a double-quoted string context (if the file is not strictly parsed as such) or relies on backticks which have special meaning in some shells but are treated as literal characters in PHP strings depending on context.

More critically, strpbrk returns the position of the first match or false if none exists. Comparing it directly to FALSE works, but be aware that strpbrk returning a string starting with 0 might behave unexpectedly in boolean contexts if not cast carefully, though PHP handles this reasonably well here.

The second part, !ctype_digit($hostName), ensures the host isn't just numbers. The regex ^([a-z0-9-]+)$ restricts characters to lowercase letters, numbers, and hyphens. Finally, ereg("^[^-]",$hostName) and ereg("[^-]$",$hostName) ensure the domain doesn't start or end with a hyphen.

While this logic is sound for basic hostname validation, using deprecated regex functions means you lose access to improved error reporting regarding invalid patterns. If you are building a production application, consider migrating this to preg_match and ctype_alnum equivalents to align with current PHP standards.

Best Practices for Reuse

The most robust way to handle cross-file function calls is to ensure your helper files explicitly declare their functions in the global scope if intended for broad use, or define them within a specific namespace if you are targeting a modular architecture. When using require_once, always place the call at the top of the file to prevent redefinition errors in case the file is included multiple times during script execution.

If you are working on a Laravel project, note that modern frameworks encourage service classes or trait-based utilities over loose global functions. However, for small scripts or legacy maintenance, the require_once pattern remains a valid and efficient method for sharing logic without coupling files too tightly.

Our network