Jump to content

An question about include()


chris-newman

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.
Link to comment
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.
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.