mkosmosports Posted October 16, 2006 Share Posted October 16, 2006 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 Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/ Share on other sites More sharing options...
brendandonhue Posted October 17, 2006 Share Posted October 17, 2006 http://php.net/require Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-109824 Share on other sites More sharing options...
mkosmosports Posted October 17, 2006 Author Share Posted October 17, 2006 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! Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-109829 Share on other sites More sharing options...
mkosmosports Posted October 17, 2006 Author Share Posted October 17, 2006 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. Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-109864 Share on other sites More sharing options...
Masna Posted October 17, 2006 Share Posted October 17, 2006 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? Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-109865 Share on other sites More sharing options...
mkosmosports Posted October 17, 2006 Author Share Posted October 17, 2006 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.... Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-110010 Share on other sites More sharing options...
craygo Posted October 17, 2006 Share Posted October 17, 2006 set a variable in your function. Instead of just a function have it require the variable[code]<?phpfunction 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 Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-110033 Share on other sites More sharing options...
mkosmosports Posted October 17, 2006 Author Share Posted October 17, 2006 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... Link to comment https://forums.phpfreaks.com/topic/24163-linking-to-an-external-php-file/#findComment-110047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.