Input loop: Difference between revisions

added php
(added php)
Line 263:
=={{header|Perl}}==
The angle brackets operator ( <code><...></code> ) reads one line at a time from a filehandle in scalar context:
<perl>open FH, "< $filename" or die "can't open file: $!";
while (my $line = <FH>) {
chomp $line; # removes trailing newline
# process $line
Line 272:
Or you can get a list of all lines when you use it in list context:
<perl>@lines = <FH>;</perl>
 
=={{header|PHP}}==
<php>$fh = fopen($filename, 'r');
if ($fh) {
while (!feof($fh)) {
$line = rtrim(fgets($fh)); # removes trailing newline
# process $line
}
fclose($fh);
}</php>
 
Or you can get an array of all the lines in the file:
<php>$lines = file($filename);</php>
 
Or you can get the entire file as a string:
<php>$contents = file_get_contents($filename);</php>
 
=={{header|Python}}==
Anonymous user