Archive for the ‘Coding’ Category

How to read from stdin

December 23rd, 2011 | admin

Reading from stdin is not something familiar to many php coders, so here are a few ways to read in the puzzle you are given from stdin using the PHP CLI.

$s=fgets(STDIN);

Reads in a complete line of input as a string, complete with end of line character.

$c=fgetc(STDIN);

Reads in a single character.

Also worth looking at are fscanf(), fgetcsv(), fread()

To test your scripts you can use this command from a linux shell (other operating systems may differ)

php your_script.php < test.txt

Where test.txt is a plain text file containing your sample input.

If you want to join codegolf.com's challenages via PHP language, you need to know how to read from stdin via PHP CLI.

codegolf.com forum