Jump to content

Archived

This topic is now archived and is closed to further replies.

SammyP

INCLUDEd Functions not available

Recommended Posts

Maybe I'm missing something here but I simply can't work this one out. I have a file which runs along outputting what I ask it to. At the top there are two INCLUDE statements which work fine and I can access their functions.

Later on I include a test file.

[code]<?php

echo "INCTEST<br/>";
sam();
testval(8);

function sam() {
  echo "INCTEST_SAM<br/>";
}

function testval($round) {
  echo "INCTEST_$round<br/>";
}

?>[/code]

This runs and produces
[pre]INCTEST
INCTEST_SAM
INCTEST_8[/pre]
as I expected.

However add the three echo statements to the main file and it has never heard of the functions, and errors.

Moving the INCLUDE to the top of the file doesn't help either. Is there something subtle or even obvious that I'm missing here.

Thanks, Sam.

Share this post


Link to post
Share on other sites
[b]edited[/b]

This is  a function ok

<?

$name="john";

function myname($name) {

echo $name;

}

myname($name);
?>




if i wanted a function from another page you do it like this ok


function.php

<?
$name="john";
function myname($name) {

echo $name;

}



?>





on the calling page do this ok.

calling_function.php

<?

include("function.php");

// run the function from the include ok.

myname($name);

?>

//You do not use an echo to output a function use my examples to study function ok.


Theres nothink to understand as it selfexsplained ok.


Thank you wildteen88.

Share this post


Link to post
Share on other sites
OK that firstly won't quite work as you expect as $name is not defined in calling_function.php so it is calling the function without an actual parameter, so it will not echo anything. However in your code when you actually Include the file it calls the function from within as well so that is why it echoes. Add 'echo "Test";' between the include and calling the function and see where the word 'john' appears.

And I do get Includes to work. In this file I have an Included file which not only works but also Includes another file. It is simply this one which does not. I was looking for clues as to why my code wasn't working. I think it must be a little more subtle. Hoepfully someone can help.

Share this post


Link to post
Share on other sites
I think what redarrow was trying to say is make sure you have included your functions first before using them. Ideally you are supposed to define your functions first before you use them, just like you do with a variable. You cannot use a variable you have defined the variable.

So basically instead of doing this:
[code=php:0]function_name('something');

include 'functions.php';[/code]


Do this:
[code=php:0]include 'functions.php';

function_name('something');[/code]

Share this post


Link to post
Share on other sites
I guarantee I am not trying to call functions before the Include statement. I even double checked.

Here is the code in the outer file:

[code]  include 'inctest.php';

  echo "INCTEST<br/>";
  sam();
  testval(8);

  if (function_exists("testval")) {
    testval(55);
  } else {
    echo "Nope.";
  }[/code]

The code inside the Include does run once but then the functions fail outside, immediately after, when it tries to call sam().

Thanks again.

Share this post


Link to post
Share on other sites
Looks fine to me. Whats in inctest.php? ALso is inctest.php is the same working directory your script is in?

Share this post


Link to post
Share on other sites
you include in the page you want to get the function to work not in the function page did you see all the examples on here .

Share this post


Link to post
Share on other sites
Just quickly, yes I know the include has to be in the calling file and though the scripts are in different directories, it doesn't matter (I edited out the directories as it isn't relevant). The Include statement is working as is evidenced by the correct output when the Include statement is run. It is only after this that the functions are not available from the calling page.

I guess I'll have to actually put all the functions into the main page, I was just hoping to have them in a library as a few pages will need to use them.

Share this post


Link to post
Share on other sites
are you pointing the include to the correct directory then.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.