0

Just trying to tidy up a little bit of code here. I have a php command to output some html. However, one of my comands is quite a large amount of html, I was wondering if it's possible to output code referenced in a different file?

For example, my current php looks like this:

$output .= '<div class="contact-form '.$css_class.'" >';
$output .= '<h4 class="form-title">'.$title.'</h4>';                    
$output .= 'SOME VERY LONG CODE'

Is it possible to do something like this:

$output .= include('file_with_long_code.html');

instead? I aven't tested this, but am curious to know if it works or what the proper way of doing it is

6
  • 1
    Have a look at no1.php.net/file_get_contents Commented Sep 29, 2014 at 11:06
  • 1
    why not use NOWDOC/HEREDOC? Commented Sep 29, 2014 at 11:06
  • 1
    $output .= file_get_content('file_with_long_code.html'); Commented Sep 29, 2014 at 11:06
  • file_get_contents won't parse his vars... Commented Sep 29, 2014 at 11:07
  • @DarkBee - An html file shouldn't have any vars that need parsing Commented Sep 29, 2014 at 11:07

4 Answers 4

1

you can instead use getfilecontent function of php

 $output .=  file_get_contents('file_with_long_code.html'); 
Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this:

    ob_start();
       include('somefile.php');
    $output = ob_get_contents();

Read more about output buffering in the docs: http://php.net/manual/en/function.ob-start.php

I recommend using a PHP Framework, most of them have a very good functionality for these Kinds of "Problems".

Comments

0

html file.tpl.php:

<div class="contact-form <?=$css_class; ?>" >
<h4 class="form-title"><?=$title; ?></h4>
SOME VERY LONG CODE

main file:

<?php

ob_start();
include('file.tpl.php');
$output = ob_get_contents();
ob_end_clean();

?>

Comments

-1

$output .= file_get_contents('file_with_long_code.html');

Yes, it is possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.