Php pass parameter to include

passing parameters to php include/require construct

There isn’t a way to pass parameters to include or require.

However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

That said, I wouldn’t suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you’ve included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It’s much more flexible, and generally a better programming technique.

Read More

In the past, many people have disagreed with this approach. But I think it is a matter of opinion.

You can’t pass _GET or _POST param via a require() or include() , but you can you first set a _SESSION key/value and pull it on the other side.

You could have the required file return an anonymous function, and then call it immediately after.

//required.php $test = function($param) < //do stuff >return $test 
//main.php $testing = require 'required.php'; $testing($arg); 

kmuenkel 2543

Include with parameters

This is something I’ve used on my recent WordPress project

Make a function functions.php :

function get_template_partial($name, $parameters) < // Path to templates $_dir = get_template_directory() . '/partials/'; // Unless you like writing file extensions include( $_dir . $name . '.php' ); >

Get parameters in cards-block.php :

// $parameters is within the function scope $args = array( 'post_type' => $parameters['query'], 'posts_per_page' => 4 ); 

Call the template index.php :

get_template_partial('cards-block', array( 'query' => 'tf_events' )); 

If you want a callback

For example, the total count of posts that were displayed:

Change functions.php to this:

function get_template_partial($name, $parameters) < // Path to templates $_dir = get_template_directory() . '/partials/'; // Unless you like writing file extensions include( $_dir . $name . '.php' ); return $callback; >

Change cards-block.php to this:

// $parameters is within the function scope $args = array( 'post_type' => $parameters['query'], 'posts_per_page' => 4 ); $callback = array( 'count' => 3 // Example ); 
$cardsBlock = get_template_partial('cards-block', array( 'query' => 'tf_events' )); echo 'Count: ' . $cardsBlock['count']; 

CodeUK 391

More Answer

  • PHP passing parameters while creating new object, call_user_func_array for objects
  • Passing named parameters to a php function through call_user_func_array
  • PHP Class construct with three optional parameters but one required?
  • Passing multiple parameters by POST using ajax to php
  • PHP Require and Include GET
  • PHP — Best Practice for Passing Variables to Include Files
  • PHP passing parameters via URL
  • Python script calling PHP script while passing $_GET parameters
  • Does PHP 7 throw an error when passing too few parameters to a function?
  • PHP Passing $_FILES and $_POST as parameters to function
  • How to call a bash script from php passing parameters
  • Why does a PHP function with no parameters require parentheses?
  • PHP function design: passing parameters
  • Composer require runs out of memory. PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted
  • Prevent direct access to a php include file
  • Passing an Array as Arguments, not an Array, in PHP
  • How to call function of one php file from another php file and pass parameters to it?
  • How do I pass parameters into a PHP script through a webpage?
  • PHP passing $_GET in the Linux command prompt
  • PHP Function with Optional Parameters
  • Are PHP include paths relative to the file or the calling code?
  • PHP pass variable to include
  • Passing JavaScript array to PHP through jQuery $.ajax
  • PHP include relative path
  • How to include a PHP variable inside a MySQL statement
  • Force composer to require PHP Version between Version X and Version Y
  • In PHP (>= 5.0), is passing by reference faster?
  • Form submit with AJAX passing form data to PHP without page refresh
  • Slim PHP and GET Parameters
  • Can I include code into a PHP class?
  • How to require PHP files relatively (at different directory levels)?
  • Include PHP inside JavaScript (.js) files
  • Passing parameters to PHPUnit
  • root path doesn’t work with php include
  • How to get multiple parameters with same name from a URL in PHP
  • Passing $_GET parameters to cron job
  • Why include when you can require in PHP?
  • Passing JSON data from php to html-data attribute and then to Javascript
  • What does the dot-slash do to PHP include calls?
  • Passing static methods as arguments in PHP
  • How can I pass parameters from the command line to $_POST in a PHP script?
  • PHP, pass parameters from command line to a PHP script
  • Shell run/execute php script with parameters
  • Passing an instance method as argument in PHP
  • How to include a class in PHP
  • Does PHP allow named parameters so that optional arguments can be omitted from function calls?

More answer with same ag

  • Get PHP SOAP location and uri from existing wsdl
  • Syntax for «RETURNING» clause in Mysql PDO
  • How to include config.php efficiently?
  • PHP float bug: PHP Hangs On Numeric Value
  • Is Magento’s Cron fundamentally flawed?
  • Regex as first line of defense against XSS
  • Safe alternatives to PHP Globals (Good Coding Practices)
  • PHP performance
  • My SilverStripe module overrides other modules display
  • xml empty tags with PHP DOMDocument
  • PHP_VERSION_ID is int but not defined. (PHP-FPM 5.4.4)
  • Visual Studio Code can’t find PHP function — maybe «require_once» syntax?
  • Export Orders from Magento for shipment
  • PHP Forms Radio Buttons
  • Codeception test fails even though try-catch
  • mutex with PHP 5.3.8
  • Undefined offset error, but offset is not undefined
  • PHP OOP Question
  • xml parse error: ‘Invalid character’
  • How can i get $cm->id based on the $courseId In Moodle 2.6.0 Version?
  • How to calculate difference of two string dates in days using PHP?
  • PHP late static binding doesn’t work correctly
  • No route found for «GET /user/register»: Method Not Allowed (Allow: POST)
  • Duplicate an AR record & re-insert this into the database
  • Is it possible to set multiple variables with one ternary operator?
  • How to replace jquery variable values with php array values
  • laravel 5.3: ErrorException in UrlGenerator.php line 314: Route [] not defined
  • Undefined date_diff()
  • DateTimeZone error: Unknown or bad timezone
  • Timestamp for different timezones in php
  • I want to cURL google search result in php
  • How to prevent PHP sessions being shared between different apache vhosts?
  • jquery mobile is ruining my POST
  • Symfony Optional constraints not working as expected
  • Using INSERT INTO.. ON DUPLICATE KEY UPDATE on Doctrine
  • Can I use a @see on a @method row in PHPDoc?
  • How to output the response HTML data by a jQuery AJAX request?
  • Writing you own media library: where to start?
  • TLS Session Resumption in php
  • After login, should all pages be https?
  • Anyone knows something like RSPec for PHP?
  • PHP Class Scope
  • Find if key exists in array
  • Passing large amounts of data from one page to another without POST?
  • Are Dynamic Prepared Statements Bad? (with php + mysqli)
  • Check if array key exists that matches regex
  • Memory optimization in PHP array
  • What is this alternative if/else syntax in PHP called?
  • regex pattern extract quotes
  • Convert Time String to Decimal Hours PHP

Источник

passing parameters to php include/require construct

There isn’t a way to pass parameters to include or require.

However the code that is included joins the program flow at the point where you include it, so it will inherit any variables that are in scope. So for example if you set $myflag=true immediately before the include, your included code will be able to check what $myflag is set to.

That said, I wouldn’t suggest using that technique. Far better for your include file to contain functions (or a class) rather than code that gets run straight off. If you’ve included a file containing functions then you can call your functions with whatever parameters you want at any point in your program. It’s much more flexible, and generally a better programming technique.

Solution 2

Include with parameters

This is something I’ve used on my recent WordPress project

Make a function functions.php :

function get_template_partial($name, $parameters) < // Path to templates $_dir = get_template_directory() . '/partials/'; // Unless you like writing file extensions include( $_dir . $name . '.php' ); >

Get parameters in cards-block.php :

// $parameters is within the function scope $args = array( 'post_type' => $parameters['query'], 'posts_per_page' => 4 ); 

Call the template index.php :

get_template_partial('cards-block', array( 'query' => 'tf_events' )); 

If you want a callback

For example, the total count of posts that were displayed:

Change functions.php to this:

function get_template_partial($name, $parameters) < // Path to templates $_dir = get_template_directory() . '/partials/'; // Unless you like writing file extensions include( $_dir . $name . '.php' ); return $callback; >

Change cards-block.php to this:

// $parameters is within the function scope $args = array( 'post_type' => $parameters['query'], 'posts_per_page' => 4 ); $callback = array( 'count' => 3 // Example ); 
$cardsBlock = get_template_partial('cards-block', array( 'query' => 'tf_events' )); echo 'Count: ' . $cardsBlock['count']; 

Solution 3

You could have the required file return an anonymous function, and then call it immediately after.

//required.php $test = function($param) < //do stuff >return $test 
//main.php $testing = require 'required.php'; $testing($arg); 

Источник

Читайте также:  What is python django
Оцените статью