search
HomeBackend DevelopmentPHP TutorialImplementing static PHP website pages based on ob series functions

Implementing static PHP website pages based on ob series functions

Pre-converting the PHP execution page into HTML is one of the so-called PHP static methods.

Other methods include template replacement, opcache and other methods.

The role of staticization:

Improve the response speed of the website and reduce the load of the server;
Used for search engine SEO;
Use PHP output buffer to write directly into html, timing Scanning the website regularly in the background is a relatively easy way to achieve static web pages.

Before staticization:

<?php
for($i=0;$i<1000;$i++){
 echo "bull<br>";
}
?>

Staticization:

<?php
ob_start();
for($i=0;$i<1000;$i++){
 echo "bull<br>";
}
$str = ob_get_contents();
ob_end_clean();
$fp = fopen("test.html","w");
fwrite($fp,$str);
fclose($fp);
echo "<p>success write</p>";
?>

This code will pre-form a test.html. To put it bluntly, it actually exchanges space for time, like news For this kind of website, the published content will basically not change after it is written, so it is more suitable to be statically converted into HTML.

When users visit, they just need to direct to this html page.

Simply use apache benchmark to test the effect:

ab.exe -n1000 -c10

## The two are above 90% During the connection, the difference is at least 150ms. Compared with complex php, html still saves a lot of calculations.

Attached is a brief introduction to related functions

1.

Flush: Refresh the contents of the buffer and output. Function format: flush()
Description: This function is frequently used and is very efficient.

2,

ob_start : Open the output buffer Function format: void ob_start(void)
Description: When the buffer is activated, all non-file headers from the PHP program None of the information is sent, but is saved in an internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.

3,

ob_get_contents : Return the contents of the internal buffer. Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.

4,

ob_get_length: Returns the length of the internal buffer. Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.

5,

ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer. Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).

6,

ob_end_clean: Delete the contents of the internal buffer and close the internal buffer Usage method: void ob_end_clean(void)
Description: This function will not output the internal The contents of the buffer but delete it!

7,

ob_implicit_flush: Turn on or off absolute refresh Usage method: void ob_implicit_flush ([int flag])
Description: Anyone who has used Perl knows $|= The meaning of ).

Related learning recommendations:

PHP programming from entry to proficiency

The above is the detailed content of Implementing static PHP website pages based on ob series functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:jb51. If there is any infringement, please contact admin@php.cn delete
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools