| 6:19 am on Sep 3, 2002 (gmt 0)|
I am afraid that I am not quite sure what you mean. I think you will have to explain it a little more.
Do you mean you want to call a function from a different file?
| 6:42 am on Sep 3, 2002 (gmt 0)|
I was just rereading your post. If what you are trying to do is use a function from another file. I would move that function out of the first file into an .inc file. These are typically used for included files that contain a number of reusable functions. You then include that library in each file that it is required with a function such as include [php.net] or require.
| 8:47 am on Sep 3, 2002 (gmt 0)|
That's exactly what I'm talking about... although I didn't realize I needed to create a inc file in order to achieve this. What does that code look like? How will it affect the previously used function calls as in the code? Like, this will mean taking it out of file2, so now when file2 calls for the function, and it's not there but in the inc, is that a problem? Do I need to modify where it calls for the function?
Thanks for your help, we're really getting somewhere!
| 10:09 am on Sep 3, 2002 (gmt 0)|
You don't have to call it a .inc, it can be anything. .inc is a convention only, and helps developers order their files... It's a good one to follow.
| 1:50 pm on Sep 3, 2002 (gmt 0)|
Actually .inc is not a good idea, because every user can get the source code if he knows the file name, as the .inc is not parsed by the server (in usual setups).
'something.inc.php' would be better.
matrym, content of included files is treated as if it were in the original file, so you can of course call the function from anywhere you like.
| 3:05 pm on Sep 3, 2002 (gmt 0)|
ok, so I currently have a file, "file2" with multiple functions, including the one I need in "file1". They're both currently php. Can't I simply just declare that I'm calling a file from file2 in file1, and then call the function normally? Is the whole inc thing really necessary if it'll just complicate things?
Thanks for all your help guys!
| 3:28 pm on Sep 3, 2002 (gmt 0)|
|Actually .inc is not a good idea, because every user can get the source code if he knows the file name |
Not so. For security reasons/code protection, your scripts should be above the http root (where your web pages are).
Call the files whatever you like, it really makes no differnce... .inc is just a convention....
As for how to include this file... check the link in my earlier post..
| 4:06 pm on Sep 3, 2002 (gmt 0)|
But must the php or inc file have only the function in it? Most ideally, I want to leave everything the same in file2 (with the function and other functions), and I want to simply use the function in "file1" ... all without creating a new file. It seems like the simplest thing... but I'm not sure how to specify a function within a file that i'm "include"ing.
| 4:13 pm on Sep 3, 2002 (gmt 0)|
Once you've included the file it's as if it is part of the file that it is included in...
| 5:00 pm on Sep 3, 2002 (gmt 0)|
Nick: Of course one should do that, but you can't expect a newbie to know that. Naming them (.inc).php is surely secure enough for first steps in PHP and does not further complicate things.
matrym: Yes, of course you can leave all the other stuff in file2!
Just do include('file2'); and you can call one, any or all of file2's functions in file1 wherever you like, the usual way, as if all of file2's contents were in file1. Try it out, you'll see how it works.
| 7:16 pm on Sep 3, 2002 (gmt 0)|
If you have a set of functions that you will use on most or many pages, they should go into a library file. Keeping in mind previous comments call it
Then you include this at the top of every file on the site. Naturally, a function that will be used on only one page might or might not go in here depending on whether you want to emphasize run-time efficiency or code reuse.
| 6:33 am on Sep 5, 2002 (gmt 0)|
I generally code PHP in .html files that the user would call, and then I code PHP in .php files for private things I call from those .html files or other .php files. This way I can have my code in any directory and not worry about hiding in several directories up.
I also include the following code in the top of my PHP files with my own custom functions so that if I include the code by accident in multiple files (for example global functions or database setup routines) then the code doesn't cause the dreaded function already defined error.
Just my 2 cents. I hope my knowledge helps neaten up someone’s code. :)
| 6:47 am on Sep 5, 2002 (gmt 0)|
another thought instead of include
" The require_once() [php.net] statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again."