How PHP puts the output into a variable

Source: Internet
Author: User
Keywords Php
I don't know if the title of my question is properly described.

I'm working on a project that uses some PHP + HTML-formatted code, and then I need to save all of the output HTML results to a cache.

My only solution now is to write the php+html format code that I want to save as a similar

$cache = "html". php. " HTML ";
The $cache variable can then be cached.

Is there a way for me to put these PHP + HTML code in a file, and then read the effects of their output into a variable?

For example I have such a few code

'" onmouseout="this.style.cssText='background: #333;'">     

ID,'post_describe',true);if(!$post_describe){echo strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 142, '');}else{echo $post_describe;}?>

There are some statements like if in the code, I'm not convenient. They all write in one line and then throw a variable inside. This is not conducive to late revision.

So I'm looking for other better ways.

I found that using include directly outputs the contents of the file without returning the content to a variable.

Reply content:

I don't know if the title of my question is properly described.

I'm working on a project that uses some PHP + HTML-formatted code, and then I need to save all of the output HTML results to a cache.

My only solution now is to write the php+html format code that I want to save as a similar

$cache = "html". php. " HTML ";
The $cache variable can then be cached.

Is there a way for me to put these PHP + HTML code in a file, and then read the effects of their output into a variable?

For example I have such a few code

'" onmouseout="this.style.cssText='background: #333;'">     

ID,'post_describe',true);if(!$post_describe){echo strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 142, '');}else{echo $post_describe;}?>

There are some statements like if in the code, I'm not convenient. They all write in one line and then throw a variable inside. This is not conducive to late revision.

So I'm looking for other better ways.

I found that using include directly outputs the contents of the file without returning the content to a variable.

That's a good question. In fact, this is the direct use of PHP as a template language.

Http://php.net/manual/en/function.includ ...

Look at the Example here #6 Using output buffering to include a PHP file into a string

Content that is not bound by PHP code is output to a response, and you cannot block it, but you can only use the Redirect method to capture it.

Resolved, adding in the middle of the include

ob_start(); 你的代码$html = ob_get_contents();ob_end_clean();

Ob_start can then put the output in the buffer, rather than directly back to the user, and then through Ob_get_contents get to

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.