PHP Classes

PHP Circular Buffer: Store values in a circular buffed array

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (11 months ago) RSS 2.0 feedNot enough user ratingsTotal: 102 All time: 9,698 This week: 116Up
Version License PHP version Categories
circular-buffer 1.0.2GNU General Publi...5.5PHP 5, Data types


This class can store values in a circular buffer array.

It can keep adding values to an array with position numbers with up to a given limit.

When the array entries exceed the initial limit number, the class starts setting values from the start of the array.

Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 57
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x



        The following example demonstrates the use of the CircularBuffer class.

        It first creates a circular buffer for handling 10 items, then adds 20 items to it.
        It finally displays the contents of the circular buffer, which contains only items #11 to #20 (the
        items #1 to #10 have been overridden).

require ( 'CircularBuffer.phpclass' ) ;

    if (
php_sapi_name ( ) != 'cli' )
        echo (
"<pre>" ) ;

// Create a circular buffer of 10 elements
$buffer = new CircularBuffer ( 10 ) ;

// Fill it with 20 items
for ( $i = 0 ; $i < 20 ; $i ++ )
$buffer [] = "Item #" . ( $i + 1 ) ;

// Print the items contained in the circular buffer (this will show the string "Item #11" through "Item #20"
echo "*** Contents of the circular buffer :\n" ;

    foreach (
$buffer as $item )
"\t$item\n" ;

"*** Number of items in the circular buffer : " . count ( $buffer ) . "\n" ;
"*** Contents of the 1st circular buffer item : {$buffer [0]}\n" ;

  Files folder image Files  
File Role Description
Plain text file CircularBuffer.phpclass Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login Plain text file Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,698
This week:116Up