Wednesday, September 25, 2024
Google search engine
HomeLanguagesPHP | DsVector shift() Function

PHP | Ds\Vector shift() Function

The Ds\Vector::shift() function is an inbuilt function in PHP which is used to remove the first element from the vector and return it.

Syntax:

mixed public Ds\Vector::shift( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns the value at index 0.

Exception: This function throws UnderflowException if vector is empty.

Below programs illustrate the Ds\Vector::shift() function in PHP:

Program 1:




<?php
  
// Declare an Vector
$vect = new \Ds\Vector(["neveropen", "of", "neveropen"]);
  
echo("First element in the vector:\n");
  
// Use shift() function to remove first 
// element from vector and display it
var_dump($vect->shift());
  
?>


Output:

First element in the vector:
string(5) "neveropen"

Program 2:




<?php
  
// Declare an Vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5, 6]);
  
// Use shift() function to remove first 
// element from vector and display it
var_dump($vect->shift());
  
var_dump($vect->shift());
  
var_dump($vect->shift());
  
var_dump($vect->shift());
  
?>


Output:

int(1)
int(2)
int(3)
int(4)

Reference: http://php.net/manual/en/ds-vector.shift.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments