Jump to content

Archived

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

mkosmosports

linking to an external php file...

Recommended Posts

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

Share this post


Link to post
Share on other sites
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!

Share this post


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

Share this post


Link to post
Share on other sites
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 [i]all[/i] variables you declare in a function will be unset at the end of the function. For example:

[code]function myFunc(){
     $var = true;
}

myFunc();
echo $var; //outputs 1[/code]

Understand?

Share this post


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

Share this post


Link to post
Share on other sites
set a variable in your function. Instead of just a function have it require the variable

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

$name = "johnny";
myname($name);
?>[/code]

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

Ray

Share this post


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

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.