Jump to content


Photo

INCLUDEd Functions not available


  • Please log in to reply
9 replies to this topic

#1 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 17 July 2006 - 04:58 PM

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.

<?php

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

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

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

?>

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.


If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 July 2006 - 05:02 PM

edited

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 17 July 2006 - 05:05 PM

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.


If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 July 2006 - 05:14 PM

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:
function_name('something');

include 'functions.php';


Do this:
include 'functions.php';

function_name('something');


#5 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 17 July 2006 - 05:33 PM

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:

  include 'inctest.php';

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

  if (function_exists("testval")) {
    testval(55);
  } else {
    echo "Nope.";
  }

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

Thanks again.

If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 July 2006 - 05:36 PM

Looks fine to me. Whats in inctest.php? ALso is inctest.php is the same working directory your script is in?

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 July 2006 - 05:37 PM

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 .
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 July 2006 - 05:40 PM


function sam($sam);

not

function sam();

try it ok
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 17 July 2006 - 05:44 PM

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.

If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 July 2006 - 05:48 PM

are you pointing the include to the correct directory then.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users