How to Call Function of One PHP File from Another PHP File and Pass Parameters to It

How to call php function from another file?

Change index.php as follows:

<?php
include "function.php";
myCustomFunction();
?>

(this is based on the assumption that both files are in the same directory, otherwise you have to add the filepath in the include line)

How to call a php function from another php file

Use the right kind of quotes around your string for the include. " or ' not and .

The use of typographic quotes like that suggests you may be trying to write code with a word processor. Don't do that; get a text or code editor instead.

How would I call a function in another file?

Include the file before you call the function.

include 'error-status.php';
validateHostName('myhostname');

Include a php file and call it from another php file

You can put all the code that's the same into an include file, e.g. common.php.

a.php

$a=1;
$b=2;
include 'common.php';

b.php

$a = 7;
$b = 8;
include 'common.php';

How do you conditionally call one php script from another with parameters

To pass control to a new script, you could use the header function. using this approach, you'd have to ensure that no other output is being generated by the first script prior to passing control, otherwise header will throw an error.

if($test){
$var = 5;

header('Location: http://www.example.com/2.php?var=' . $var);
}

Then...

2.php
<?php
$varFrom1 = $_GET['var'];

Alternatively, I recommend using the include statement which lets you bring in another file and execute the functions contained therein

include '2.php';

$variable = "something";

if($test){
functionIn2($variable);
}

You can reference it here: http://php.net/manual/en/function.include.php



Related Topics



Leave a reply



Submit