Thursday, October 23, 2025
HomeLanguagesPHP | DsSequence allocate() Function

PHP | Ds\Sequence allocate() Function

The Ds\Sequence::allocate() function is an inbuilt function in PHP which is used to allocate enough memory for required capacity.

Syntax:

void abstract public Ds\Sequence::allocate ( int $capacity )

Parameter: This function accepts single parameter $capacity which indicate number of capacity allocated.

Return value: This function does not return any values.

Below programs illustrate the Ds\Sequence::allocate() function in PHP:

Example 1:




<?php
  
// Create new sequence
$seq = new \Ds\Vector();
  
// Use capacity() function to 
// display the capacity
var_dump($seq->capacity());
  
// Allocate capacity
$seq->allocate(50);
  
// Display capacity
var_dump($seq->capacity());
  
// Allocate capacity
$seq->allocate(80);
  
// Display capacity
var_dump($seq->capacity());
?>


Output:

int(8)
int(50)
int(80)

Example 2:




<?php
  
// Create new sequence
$seq = new \Ds\Vector();
  
// Declare capacity array
$arr = array(10, 20, 30, 40);
  
// Loop run for every array element  
foreach ($arr as $val) {  
      
    // Allocate capacity
    $seq->allocate($val);
  
    // Display capacity
    var_dump($seq->capacity());
}
  
?>


Output:

int(10)
int(20)
int(30)
int(40)

Reference: http://php.net/manual/en/ds-sequence.allocate.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS