Forum Moderators: coopster

Message Too Old, No Replies

Generic functions inclosed with <?php ...?>

The wrong thing to do?

         

neophyte

8:09 am on May 16, 2005 (gmt 0)

10+ Year Member



I'm trying to split out all of my generic functions into seperate include files for the sake of re-usability.

Functions like form validation, getting stuff into and out of a DB table, etc.

Each of these functions (which reside in a seperate .php file) start are like this:

<?php
function something() {
do some stuff...
}
?>

Now, here's the thing: I'm including these functions inside another <?php ...?> block (usually some conditional statement).

Since that's the case, I guess that when the script runs, this is what is going on:

<?php
some conditional statement {
<?php
function something () {
do some stuff...
}
?>
}
?>

Have I lost everyone with this example? Do you see what I mean?

What I've been doing works fine, but is it the right way to write external/included functions?

I guess I could have made this entire question a lot shorter by asking: If you create a generic function that is meant for "inclusion", do you start and end that function with <?php ...?>, or do you just start it with function something ()?

Neophyte

jatar_k

4:04 pm on May 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



all my included files start with <? and end with?>, I use short tags so yours should all start with <?php

also, why all the generic functions in seperate files? You could group functions that are similar together.

neophyte

1:57 am on May 17, 2005 (gmt 0)

10+ Year Member



jatar_k -

Okay, glad I'm doing it correctly.

Actually, I do group my functions in seperate files, like all my text-handling functions go into a file named text.inc.php.

Thanks for the clarification in regards to my question.

PieSocial

3:52 am on May 17, 2005 (gmt 0)

10+ Year Member



The way I do it is this, the top of all my PHP files which are to include something look like this:

<?php
include('includes.php');
?>

Then my includes.php file looks like this:

<?php
include('file1.php');
include('file2.php');
etc...
?>

Each included file looks like this:

<?php
//Function: Do something
function something() {
//function script
}

//Function: Another function
function somethingelse() {
//function script
}
?>

This is just the way I do it, find it works out great. Good luck! Some people use classes, but I don't see the point in small PHP scripts. I find it's much easier to just be able to use something() in my scripts after include('includes.php').

I don't know if there's a better way.

Cheers.

webkami

4:25 pm on May 18, 2005 (gmt 0)

10+ Year Member



<?php
some conditional statement {
<?php
function something () {
do some stuff...
}
?>
}
?>

This is funny......

use "require_once" for functions, it would NOT iclude the <?php into <?php but just compile the functions to make them ready for your use.

e.g.
require_once("functions.php");