Jump to content

linking to an external php file...


mkosmosports

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.