InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Reading from the Keyboard in PHP
PHP

Reading from the Keyboard in PHP

InfinityCoder December 27, 2016

You need to read in some typed user input.

Read from the special filehandle STDIN:

1
2
3
4
5
6
7
8
9
10
11
12
13
print "Type your message. Type '.' on a line by itself when you're done.\n";
 
$last_line = false; $message = '';
while (! $last_line) {
  $next_line = fgets(STDIN,1024);
  if (".\n" == $next_line) {
      $last_line = true;
  } else {
      $message .= $next_line;
  }
}
 
print "\nYour message is:\n$message\n";

If the Readline extension is installed, use readline():

1
2
3
4
5
6
7
8
9
10
11
$last_line = false; $message = '';
while (! $last_line) {
  $next_line = readline();
  if ('.' == $next_line) {
      $last_line = true;
  } else {
      $message .= $next_line."\n";
  }
}
 
print "\nYour message is:\n$message\n";

If the ncurses extension is installed, use ncurses_getch():

1
2
3
4
5
6
7
8
9
10
$line = '';
ncurses_init();
ncurses_addstr("Type a message, ending with !\n");
/* Display the keystrokes as they are typed */
ncurses_echo();
while (($c = ncurses_getch()) != ord("!")) {
   $line .= chr($c);
}
ncurses_end();
print "You typed: [$line]\n";

With the special filehandle STDIN, you can use all the standard file-reading functions to process input (fread(), fgets(), etc.).

The Solution uses fgets(), which returns input a line at a time. If you use fread(), the input still needs to be newline terminated to make fread() return.

For example, if you run:

1
2
$msg = fread(STDIN,4);
print "[$msg]";

and type in tomato and then a newline, the output is [toma]. The fread() grabs only four characters from STDIN, as directed, but still needs the newline as a signal to return from waiting for keyboard input.
The Readline extension provides an interface to the GNU Readline library. The read line() function returns a line at a time, without the ending newline.

Readline allows Emacs- and vi-style line editing by users. You can also use it to keep a history of previously entered commands:

1
2
3
4
5
6
7
8
9
$command_count = 1;
while (true) {
  $line = readline("[$command_count]--> ");
  readline_add_history($line);
  if (is_readable($line)) {
      print "$line is a readable file.\n";
  }
  $command_count++;
}

This example displays a prompt with an incrementing count before each line.

Because each line is added to the Readline history with readline_add_history(), pressing the up and down arrows at a prompt scrolls through the previously entered lines.
The ncurses extension is an interface to the GNU ncurses library, which provides comprehensive control over keyboard events, mouse events, and screen output in text mode.
The primary way to read keyboard input with ncurses is the ncurses_getch() function, which returns the ASCII code for the key pressed.

A key difference between ncurses and the other two methods described here is that no newline is required before the
keystroke is processed.

The ncurses_getch() function returns right away after one keypress. In the example in the Solution, the code loops, repeatedly calling ncurses_getch() (and appending the typed character to $line) until ! is typed.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Specifying an Array Not Beginning at Element 0 in PHP
You want to assign multiple elements to an array in …

Specifying an Array Not Beginning at Element 0 in PHP

Exchanging Values Without Using Temporary Variables in PHP
You want to exchange the values in two variables without …

Exchanging Values Without Using Temporary Variables in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap