Jump to content

Archived

This topic is now archived and is closed to further replies.

chris-newman

An question about include()

Recommended Posts

I am wanting to keep my scripts neat and tidy and a few as I need to get the job done. I am building a search engine with elements such as forms. banners etc, some of which I want to reuse. is one of the stated benefits of languages such as PHP is I can reuse the code. I am using the include() function. However this only includes HTML code stored in a separate file. Question: Is this someway to include the HTML stored in the calling file? This will reduce the number of files I need to manage. Any help would be appreciated.

Share this post


Link to post
Share on other sites
There's no reason why an included file can't be a composite of php and html, any more than there's no reason that a file into which you want to include something can't be a composte of php and html.  I *think* that answers the quetion.

Share this post


Link to post
Share on other sites
[code]<?php
include("stuff.php");
if ($x = 6) {
    include("more_stuff.php");
} else {
    include("some_other_stuff.html");
}[/code]

Like that?

Share this post


Link to post
Share on other sites
If you want to include parts of the file, you'll want to seperate your code into functions, for example. You have a form. You put all the code for the form in a function called showForm, like so:
[code=php:0]// define the function showForm
function showForm();
{
    // we echo the HTML for the form
    // Below is HEREDOC syntax
    echo <<<HTML
<form action="file.php" method="post">
  your form fields here
</form>
HTML;
// DO NOT INDENT OR PUT ANYTHING ON THE LINE ABOVE
}[/code]

Now to call the function you use:
[code=php:0]showForm();[/code]

When you call this function, it'll produce this:
[code]<form action="file.php" method="post">
  your form fields here
</form>[/code]

Which will allow for reusable code.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=107077.msg429081#msg429081 date=1157538584]
If you want to include parts of the file, you'll want to seperate your code into functions, for example. You have a form. You put all the code for the form in a function called showForm, like so:
[code=php:0]// define the function showForm
function showForm();
{
    // we echo the HTML for the form
    // Below is HEREDOC syntax
    echo <<<HTML
<form action="file.php" method="post">
  your form fields here
</form>

HTML;
// DO NOT INDENT OR PUT ANYTHING ON THE LINE ABOVE
}[/code]

Now to call the function you use:
[code=php:0]showForm();[/code]

When you call this function, it'll produce this:
[code]<form action="file.php" method="post">
  your form fields here
</form>[/code]

Which will allow for reusable code.
[/quote]



Thanks Will give it a go  ;D ;D ;D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.