how to pass a variable through the require() or include() function of php?

how to pass a variable through the require() or include() function of php?

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

Stefan Bogdanescu

Founder & Senior Architect

Why require() Cannot Accept Query String Parameters

The error message you are seeing—Failed opening required 'diggstyle_code.php?page=1'—is a direct consequence of how PHP's require(), include(), and similar functions handle file paths. These functions expect a physical filesystem path to an existing file, not a URL or a virtual resource locator.

When you write require("diggstyle_code.php?page=$page_no"), the string inside the parentheses is treated literally as the filename. PHP attempts to open a file named exactly diggstyle_code.php?page=1. Because your filesystem does not contain a file with a question mark and query parameters in its name, the operation fails immediately. The parser does not attempt to resolve the path relative to the web server's document root or treat it as a URL; it strictly looks for that specific string on the disk.

Passing Arguments via Function Parameters

If your goal is to pass data into the included file so you can use it within its scope, you must use function arguments instead of query strings in the path. This approach leverages PHP's variable scoping rules. When a file is included, all variables defined in the calling script (provided they are not protected by private or static modifiers that prevent cross-file access) are available to the included code.

Here is how you should structure the call:

$page_no = 1; // Example value collected beforehand

require("diggstyle_code.php", $page_no);

Inside diggstyle_code.php, you can simply reference $page_no directly:

// diggstyle_code.php
echo "Current page number is: " . $page_no;

This method is robust because it decouples the logic of data passing from the file system structure. It avoids the need for additional files to handle routing, and it keeps your codebase flat without requiring complex directory structures. The trade-off here is that the included file must be designed to accept variables as global scope or via specific arguments if you wrap the include in a function.

Using Query Strings for Routing Logic

You might be tempted to use the query string format (?page=1) because it mimics standard web URL routing. While this works perfectly with $_GET when accessed via a browser request (e.g., http://yourdomain.com/page.php?page=1), it fails when used programmatically inside another PHP script.

If you strictly need to simulate a URL structure or if the included file relies on $_GET['page'], you have two options:

  1. Create an actual URL: If your application is running on a web server, you can point the include to a real URL using a function like require() (which technically requires a local path) or better yet, use include with a full URI if your server configuration supports it, though this is often unreliable across different environments.
  2. Simulate the superglobal: The most reliable pattern for internal logic that depends on query parameters is to construct the variable manually:
$page_no = isset($_GET['page']) ? $_GET['page'] : 1;
// ... pass $page_no to the included file as shown above

Security Considerations

Attempting to use require() with user input directly in the path, even without query strings, opens your application to Path Traversal vulnerabilities. If $page_no contains a value like ../../etc/passwd, the require() function will happily execute arbitrary code from that location.

When passing variables as arguments (the recommended approach), you mitigate this risk because the filename itself remains hardcoded and safe:

// Safe: Filename is hardcoded
require("diggstyle_code.php", $page_no);

// Unsafe: Never do this with user input
$filename = "diggstyle_code.php"; // If $page_no were appended here, it would be dangerous
require($filename . "?page=" . $user_input); 

Modern Alternatives with Laravel

If you are working in a modern PHP ecosystem or maintaining legacy code that needs refactoring, the imperative use of require() and include() is often replaced by dependency injection or framework-specific routing. In a Laravel application, for instance, you would rarely need to manually include files with variables. Instead, you would utilize Laravel's View Composer or pass data through the controller to a Blade template using the standard view syntax:

// Inside a Controller
return view('diggstyle_code', ['page' => $page_no]);

This pattern ensures that data is passed safely and cleanly, adhering to MVC principles rather than relying on global variable scoping or string concatenation of file paths. For more details on modern PHP practices and architectural patterns, you might explore resources like https://laravelcompany.com.

Our network