PHP readline_add_history() Function
Last Updated :
13 Sep, 2023
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();
readline_add_history( "First line" );
readline_add_history( "Second line" );
readline_add_history( "Third line" );
$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();
readline_add_history( "Line 1" );
readline_add_history( "Line 2" );
readline_add_history( "Line 3" );
$history = readline_list_history();
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();
for ( $i = 1; $i <= 5; $i ++) {
$line = "Line $i" ;
readline_add_history( $line );
}
$history = readline_list_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
Share your thoughts in the comments
Please Login to comment...