Forum Moderators: coopster

Message Too Old, No Replies

php files

using different files as functions

         

wkpride

1:40 am on Nov 17, 2007 (gmt 0)

10+ Year Member



Let's say I have a log in statement such as :

$dbh=mysql_connect ("localhost", "wkpride", "password") or die ('I cannot connect to the database because: ' . mysql_error());

I was wondering if I could save this as login.php? And then when I start another page, I could just call up my login.php, rather than copy & pasting the actual code.

the questions:

1) is it possible to call scripts that are on a different page?
2) If so, is ther a specail instruction?
3) Do people do this? - or is it a bad idea?

phparion

5:09 am on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



when you INCLUDE something it means you have ENTERED that page's code into your page which has the include statement. e.g

index.php includes login.php = index.php which is the sum of both pages.

So, of course, you can reuse your code with includes in fact that is a very vital use of includes.

BUT I will not prefer this method. It is better to learn OOP and write classes. Include class in a page and then use all functions of that class using its object.

However if you want to use your connection code as an include then you can do it, this is how a lot of people do it.

BUT make sure your include file's variables do not conflict with your other page that is including the file. That might create logical bugs.