...

View Full Version : Calling one include's function in another include.



Blocksbox
07-10-2007, 07:49 PM
I have a file like this...

(test1.php)

<?php
include("test2.php");
include("test3.php");
?>

(test2.php)

<?php
function site_welcome() {
echo 'Welcome to the site!<br>
';
}
site_welcome();
?>

(test3.php)

<?php site_welcome(); ?>

(Result)
Welcome to the site!

Fatal error: Call to undefined function: site_welcome() in test3.php on line 1

(My Question)
How do I go about calling functions from test2 in test3? Is that even possible?

guvenck
07-10-2007, 08:07 PM
In your case, test3.php does not know what site_welcome() is.

Gather all your function in a functions.php and include it in every file.

functions.php:



function site_welcome() {
echo 'Welcome to the site!<br>';
}


test3.php:



include("functions.php");
site_welcome();

Blocksbox
07-10-2007, 08:51 PM
Is there is no other way to make one function active for every include oppose to adding it to each one individually? The reason I ask is because I am currently using sessions where the user is logged in, but the file that says "Welcome, username" is in an include that deviates from the original session. Since that makes it unable to read the user, it always results in a "Welcome, Guest" printout.

Fumigator
07-11-2007, 06:53 AM
Blocksbox, this should work fine. Includes and Requires insert the included file on the line where the include is located. As long as that is not inside some other function that cannot see a function somewhere else, it will work. I just ran the following test (v.4.4.6) and the function inside t2.php was visible to t3.php.



t1.php
<?php
echo "begin t1<br />";
require("t2.php");
require("t3.php");

?>

t2.php
<?php
echo "begin t2<br />";

function testFunc() {
echo "inside testFUnc<br />";
}

?>

t3.php
<?php
echo "begin t3<br />";

testFunc();
?>

Result was:
begin t1
begin t2
begin t3
inside testFUnc



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum