Saturday, August 30, 2025
HomeLanguagesPHP readline_add_history() Function

PHP readline_add_history() Function

The readline_add_history() is an inbuilt function in PHP that is used to add a line to the command line history. 

Syntax:

readline_add_history($prompt): bool

Parameter: 

This function accepts only a single parameter that is described below.

  • $prompt: This is a string parameter that adds to a line in the command line history. It should be a string parameter.

Return Value: 

The return value of the readline_add_history() function is a boolean value. If this function successfully adds a line to the command line history, then it will return “true” otherwise on failure it will return “false”.

Program 1: The following program demonstrates the readline_add_history() Function.

PHP




<?php
readline_clear_history();
  
// Add lines to command line history
readline_add_history("First line");
readline_add_history("Second line");
readline_add_history("Third line");
  
// Retrieve command line history
$history = readline_list_history();
print_r($history);
?>


Output

Array
(
    [0] => First line
    [1] => Second line
    [2] => Third line
)

Program 2: The following program demonstrates the readline_add_history() Function.

PHP




<?php
  
readline_clear_history();
  
// Add lines to command line history
readline_add_history("Line 1");
readline_add_history("Line 2");
readline_add_history("Line 3");
  
// Retrieve the command line history
$history = readline_list_history();
  
// Print the command line history in reverse order
for ($i = count($history) - 1; $i >= 0; $i--) {
    echo "[$i] {$history[$i]}" . PHP_EOL;
}
?>


Output

[2] Line 3
[1] Line 2
[0] Line 1

Program 3: The following program demonstrates the readline_add_history() Function.

PHP




<?php
readline_clear_history();
  
// Add lines to command line history using a loop
for ($i = 1; $i <= 5; $i++) {
    $line = "Line $i";
    readline_add_history($line);
}
  
// Retrieve the command line history
$history = readline_list_history();
  
// Print the command line history
foreach ($history as $index => $line) {
    echo "[$index] $line" . PHP_EOL;
}
?>


Output

[0] Line 1
[1] Line 2
[2] Line 3
[3] Line 4
[4] Line 5

Reference: https://www.php.net/manual/en/function.readline-add-history.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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS