Jump to content

INCLUDEd Functions not available


SammyP

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.

Link to comment
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.
Link to comment
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.

Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.