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
https://forums.phpfreaks.com/topic/19878-an-question-about-include/
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.
[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

Archived

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

×
×
  • 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.