newbie question about include() and include_once() functions

newbie question about include() and include_once() functions

am 29.11.2007 20:01:58 von jpsebasti

Folks,

Real newbie here. I'm going through some tutorials and reading as much
as possible to learn PHP.

This is a simple question.

I want to put a bunch of PHP functions into a file. These functions
would be shared by a lot of different php scripts.

I want to then include() or include_once() that file with the defined
functions into each php script.

The problem I'm having is that instead of including the file and thus
defining the functions within my main script, the included files are
being displayed as text.

I read the following in a beginners tutorial named "Including Files in
PHP - Beginner Tutorial" on the phpfreaks.com site

"If you include a file, for example a plain text file that does not
have the PHP open and close tags, the file will be displayed within
the current PHP script."


So I guess I'm confused.

If I create my include file (lets call it functions.php)

function testfunct1 ($text)
{
echo "Received text " . $text . "\n";
}
?>

And then if I create the main script (lets call it main.php)

include ('functions.php');
?>

I get an error because basically, I'm nesting tags.


If I take out the tag from within functions.php, then
instead of including the function definition in main.php, the contents
of functions.php is displayed when main.php runs.

What the heck am I missing here?

Thanks in advance!

Re: newbie question about include() and include_once() functions

am 29.11.2007 20:44:00 von James Mackin

JP wrote:

> The problem I'm having is that instead of including the file and thus
> defining the functions within my main script, the included files are
> being displayed as text.

My guess is that the included file doesn't have execute permissions
meaning the code is inserted rather than executed.

Re: newbie question about include() and include_once() functions

am 29.11.2007 20:50:30 von zeldorblat

On Nov 29, 2:44 pm, James Mackin wrote:
> JP wrote:
> > The problem I'm having is that instead of including the file and thus
> > defining the functions within my main script, the included files are
> > being displayed as text.
>
> My guess is that the included file doesn't have execute permissions
> meaning the code is inserted rather than executed.

Probably not. The PHP files are not "executed" so to speak. The PHP
engine is the actual executable and merely reads the files.

The example the OP posted should work correctly. He said "I get an
error because basically, I'm nesting tags." What error are
you getting? Having the PHP tags in the include file is the correct
way to do it -- otherwise PHP doesn't know it needs to parse the file.

Re: newbie question about include() and include_once() functions

am 29.11.2007 20:54:09 von luiheidsgoeroe

On Thu, 29 Nov 2007 20:01:58 +0100, JP wrote:

> Folks,
>
> Real newbie here. I'm going through some tutorials and reading as much
> as possible to learn PHP.
>
> This is a simple question.
>
> I want to put a bunch of PHP functions into a file. These functions
> would be shared by a lot of different php scripts.
>
> I want to then include() or include_once() that file with the defined
> functions into each php script.
>
> The problem I'm having is that instead of including the file and thus
> defining the functions within my main script, the included files are
> being displayed as text.
>
> I read the following in a beginners tutorial named "Including Files in
> PHP - Beginner Tutorial" on the phpfreaks.com site
>
> "If you include a file, for example a plain text file that does not
> have the PHP open and close tags, the file will be displayed within
> the current PHP script."
>
>
> So I guess I'm confused.
>
> If I create my include file (lets call it functions.php)
>
> > function testfunct1 ($text)
> {
> echo "Received text " . $text . "\n";
> }
> ?>
>
> And then if I create the main script (lets call it main.php)
>
> > include ('functions.php');
> ?>
>
> I get an error because basically, I'm nesting tags.

Well, it may seem like that, but an include/require indeed kinf of
implicitly ends/restarts php-tags. So, what is the actual error you get?
And are you sure you use the filesystem to include it, not http?
--
Rik Wasmus

Re: newbie question about include() and include_once() functions

am 29.11.2007 22:00:44 von jpsebasti

Hi All,

Thanks for the replies so far.

Here is my exact code and the error I am receiving when I run this:

Main file: example_include.php
include ('C:\Sebastian\work\php\testfunction.php');
echo "Calling testfunction\n";
testfunction("This is a test");
?>

File: testfunction.php
funciton testfunction($text)
{
echo "Inside testfunction\n");
echo "You passed the value " . $text . " to this function.\n";
}
?>

Now at my C: prompt, I run it as follows:

C:\Sebastian\work\php>php example_include.php
PHP Parse error: syntax error, unexpected T_STRING in C:\Sebastian
\work\php\tes
tfunction.php on line 2

C:\Sebastian\work\php>

Re: newbie question about include() and include_once() functions

am 29.11.2007 22:04:50 von zeldorblat

On Nov 29, 4:00 pm, JP wrote:
> Hi All,
>
> Thanks for the replies so far.
>
> Here is my exact code and the error I am receiving when I run this:
>
> Main file: example_include.php
> > include ('C:\Sebastian\work\php\testfunction.php');
> echo "Calling testfunction\n";
> testfunction("This is a test");
> ?>
>
> File: testfunction.php
> > funciton testfunction($text)
> {
> echo "Inside testfunction\n");
> echo "You passed the value " . $text . " to this function.\n";
> }
> ?>
>
> Now at my C: prompt, I run it as follows:
>
> C:\Sebastian\work\php>php example_include.php
> PHP Parse error: syntax error, unexpected T_STRING in C:\Sebastian
> \work\php\tes
> tfunction.php on line 2
>
> C:\Sebastian\work\php>

The line:

echo "Inside testfunction\n");

Has a right-paren that shouldn't be there.

Re: newbie question about include() and include_once() functions

am 29.11.2007 22:05:51 von jpsebasti

Hi all,

I'm a big fat dummy. I just realized that besides the
error in the echo statement in testfunction.php, I mis-spelled the
word function.
It's working now.
Geeze!

Thanks for all the replies.

Cheers