Welcome to WebmasterWorld Guest from 54.226.194.180

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Function

php function

     

matrym

6:03 am on Sep 3, 2002 (gmt 0)

10+ Year Member



I'm kind of new to PHP and all... and I've got a php function in a separate file that I want to apply to this file... and I want to know how it's done? Do I have to rewrite the function in the current file, or is there a way to specify that it's a function of a separate file?

Thanks,
Brett

jatar_k

6:19 am on Sep 3, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



matrym,

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?

jatar_k

6:42 am on Sep 3, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

matrym

8:47 am on Sep 3, 2002 (gmt 0)

10+ Year Member



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!

-- Brett

Nick_W

10:09 am on Sep 3, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

[php.net ]

Nick

c3oc3o

1:50 pm on Sep 3, 2002 (gmt 0)

10+ Year Member



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.

matrym

3:05 pm on Sep 3, 2002 (gmt 0)

10+ Year Member



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!
-- Brett

Nick_W

3:28 pm on Sep 3, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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..

Nick

matrym

4:06 pm on Sep 3, 2002 (gmt 0)

10+ Year Member



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.

Thanks,
Brett

Nick_W

4:13 pm on Sep 3, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Once you've included the file it's as if it is part of the file that it is included in...

Nick

c3oc3o

5:00 pm on Sep 3, 2002 (gmt 0)

10+ Year Member



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.

ergophobe

7:16 pm on Sep 3, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

functions.php
library.inc
purple.green

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.

Tom

Lisa

6:33 am on Sep 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

if(MY_CUSTOM_FUNCTION_MAKELINK==1){
return;
}
define("MY_CUSTOM_FUNCTION_MAKELINK", 1);

Just my 2 cents. I hope my knowledge helps neaten up someone’s code. :)

jatar_k

6:47 am on Sep 5, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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."

 

Featured Threads

Hot Threads This Week

Hot Threads This Month