chris-newman Posted September 6, 2006 Share Posted September 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/ Share on other sites More sharing options...
AndyB Posted September 6, 2006 Share Posted September 6, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-86966 Share on other sites More sharing options...
chris-newman Posted September 6, 2006 Author Share Posted September 6, 2006 That wasn't quite what I meant. Can I have a file file.php with an statement that *includes* part of that file. Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-86979 Share on other sites More sharing options...
AndyB Posted September 6, 2006 Share Posted September 6, 2006 [code]<?phpinclude("stuff.php");if ($x = 6) { include("more_stuff.php");} else { include("some_other_stuff.html");}[/code]Like that? Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-86990 Share on other sites More sharing options...
wildteen88 Posted September 6, 2006 Share Posted September 6, 2006 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 showFormfunction 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 Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-86991 Share on other sites More sharing options...
chris-newman Posted September 6, 2006 Author Share Posted September 6, 2006 [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 showFormfunction 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 Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-86994 Share on other sites More sharing options...
quillspirit Posted September 6, 2006 Share Posted September 6, 2006 Yep, you are asking about functions. Quote Link to comment https://forums.phpfreaks.com/topic/19878-an-question-about-include/#findComment-87242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.