Backend Development 2 min read

PHP readline() Function: Reading a Line from User Input

This article explains the PHP readline() function, its purpose for reading a line of user input, the optional prompt parameter, return value details, and provides a complete example demonstrating reading commands, adding them to history, and displaying history and variable information.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP readline() Function: Reading a Line from User Input

The readline() function in PHP reads a line from the user input and returns it as a string without the trailing newline character.

It accepts an optional $prompt parameter, which allows you to display a custom prompt message to the user.

The function returns the entered line as a string; you must manually add the line to the history using readline_add_history() if you want it stored.

Example usage:

<?php
// get 3 commands from user
for ($i = 0; $i < 3; $i++) {
    $line = readline("Command: ");
    readline_add_history($line);
}
// dump history
print_r(readline_list_history());
// dump variables
print_r(readline_info());

This example prompts the user three times for a command, stores each command in the readline history, then prints the entire history and the current readline information.

PHPhistoryCommand-lineinputreadline
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.