SammyP Posted July 17, 2006 Share Posted July 17, 2006 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]<?phpecho "INCTEST<br/>";sam();testval(8);function sam() { echo "INCTEST_SAM<br/>";}function testval($round) { echo "INCTEST_$round<br/>";}?>[/code]This runs and produces[pre]INCTESTINCTEST_SAMINCTEST_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. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/ Share on other sites More sharing options...
redarrow Posted July 17, 2006 Share Posted July 17, 2006 [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 okfunction.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. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59495 Share on other sites More sharing options...
SammyP Posted July 17, 2006 Author Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59497 Share on other sites More sharing options...
wildteen88 Posted July 17, 2006 Share Posted July 17, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59499 Share on other sites More sharing options...
SammyP Posted July 17, 2006 Author Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59508 Share on other sites More sharing options...
wildteen88 Posted July 17, 2006 Share Posted July 17, 2006 Looks fine to me. Whats in inctest.php? ALso is inctest.php is the same working directory your script is in? Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59511 Share on other sites More sharing options...
redarrow Posted July 17, 2006 Share Posted July 17, 2006 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 . Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59513 Share on other sites More sharing options...
redarrow Posted July 17, 2006 Share Posted July 17, 2006 function sam($sam);not function sam();try it ok Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59515 Share on other sites More sharing options...
SammyP Posted July 17, 2006 Author Share Posted July 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59519 Share on other sites More sharing options...
redarrow Posted July 17, 2006 Share Posted July 17, 2006 are you pointing the include to the correct directory then. Quote Link to comment https://forums.phpfreaks.com/topic/14861-included-functions-not-available/#findComment-59522 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.