Jump to content


Photo

An question about include()


  • Please log in to reply
6 replies to this topic

#1 chris-newman

chris-newman
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 06 September 2006 - 09:22 AM

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.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 September 2006 - 09:26 AM

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.
Legend has it that reading the manual never killed anyone.
My site

#3 chris-newman

chris-newman
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 06 September 2006 - 10:05 AM

That wasn't quite what I meant. Can I have a file file.php with an statement that *includes* part of that file.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 06 September 2006 - 10:28 AM

<?php
include("stuff.php");
if ($x = 6) {
    include("more_stuff.php");
} else {
    include("some_other_stuff.html");
}

Like that?
Legend has it that reading the manual never killed anyone.
My site

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 September 2006 - 10:29 AM

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:
// 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
}

Now to call the function you use:
showForm();

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

Which will allow for reusable code.

#6 chris-newman

chris-newman
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 06 September 2006 - 10:44 AM

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:

// 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
}

Now to call the function you use:
showForm();

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

Which will allow for reusable code.




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

#7 quillspirit

quillspirit
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationOregon, USA

Posted 06 September 2006 - 04:59 PM

Yep, you are asking about functions.
Peace,
Shawn
Simple PHP




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users