Jump to content


Photo

linking to an external php file...


  • Please log in to reply
7 replies to this topic

#1 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 16 October 2006 - 11:58 PM

Hey Everyone,

How can I link to an external php file containing functions that I want to be able to call on at any point in time?

PS. Sorry about the strange original post. Im having some issues posting.

Thanks!

mkosmosports



#2 brendandonhue

brendandonhue
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 17 October 2006 - 12:37 AM

http://php.net/require

#3 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 17 October 2006 - 01:08 AM

Hmm...still having issues with this. I have a file sitting in the same directory as the html file, called "func.php" Inside I have a simple function:
<?
function writeMyName()
  {
  echo "Jim Johnson";
  }
?>
Then, in my html page I say:
<?php
include("func.php");
                writeMyName();
?>
This returns an error however, saying "Fatal error: Call to undefined function writeMyName() in" If I simply wanted to call on this function which writes my name, how would I do it. Can someone be kind enough to simply write it out so I could understand it?

Thanks!

#4 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 17 October 2006 - 04:54 AM

Alright, I got it to work, but is this true that everytime you write a new function, you need to rewrite all variables and parameters used in the function??!! How come they are not retained? Is there a way I can just declare each variable once per html page and not need to call on them everytime they need to be used?

Thanks.

#5 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 17 October 2006 - 05:00 AM

You could use GLOBAL. But I suggest, more so, that you simply don't use functions (no, you don't have to). Variables defined in a function's parameters will not exist outside of that particular function. Makes sense to me, what about you?

This doesn't mean that all variables you declare in a function will be unset at the end of the function. For example:

function myFunc(){
     $var = true;
}

myFunc();
echo $var; //outputs 1

Understand?
I like LoL.

#6 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 17 October 2006 - 01:13 PM

Mornin Masna,

I do understand what you mean, I appreciate your help. The thing is, I have a lot of duplicate scripts in several html files. These scripts do use quite a few variables that would need to be repeated constantly and I dunno if turning on GLOBAL is a good idea. Is it a reasonable solution to include the scripts that get duplicated in a separate php file each (would give me around 5-8 separate php files and then simply call on the different files when needed?

Thanks for you expert advice....


#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 17 October 2006 - 01:53 PM

set a variable in your function. Instead of just a function have it require the variable

<?php
function myname($var){
   echo "My name is $var";
}

$name = "johnny";
myname($name);
?>

Then on another page if you have a different name assigned to a variable you can use it.

Ray

#8 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 17 October 2006 - 02:07 PM

Thanks craygo for your advice. For the needs of my project however, it looks like storing the duplicate html/php code in separate php files and the simply calling on it via include is working out perfectly (without the use of functions)! Ive got one separate php file per duplicate script. This will save me so much time, and its working flawlessly...

Do you see any disadvantages to doing this? The only one I see is that I need separate php files for each piece of code that needs to be used (Although I have heard from others that including one php file per html page is actually not a good idea, so maybe this is not a disdvantage. From a workload standpoint it is at most a minor inconvenience anyhow.)

Thanks...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users