ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÓÃPHP½Å±¾ÔÚLinuxÉ϶ÁÈ¡ÊäÈëºÍ¶ÔÎÄ

·¢²¼Ê±¼ä:2007-03-20 00:18:39À´Ô´:ºìÁª×÷Õß:Worktogether
¡¡Perl×÷ΪLinuxϵͳÃüÁîÐнű¾µÄÊ×Ñ¡½âÊÍÓïÑÔµÄÈÕ×Óһȥ²»¸´·µÁË¡£½ñÌ죬ÎÒÃÇÓÐÁ˸ü¶àµÄÑ¡Ôñ£¬°üÀ¨Python¡¢RubyºÍPHP¡£Èç¹ûÄãÒѾ­ÎªÍøÕ¾±àдPHP´úÂ룬²¢ÊìÚÏÕâÖÖÓïÑÔ£¬ÄÇôÄã»á·¢ÏÖ£¬ÔÚÃüÁîÐÐʹÓÃPHPµÄËÙ¶ÈÖ®¿ì£¬Ð§¹ûÖ®ºÃÁîÈ˾ªÆæ¡£

Ôڽű¾ÖУ¬Èκνű¾ÓïÑÔµÄÒ»¸ö×î´ó¹¦ÓÃÊǶÔÎļþ½øÐвÙ×÷²¢»ñÈ¡Óû§ÊäÈë¡£PHP´¦ÀíÕâЩ˿ºÁ²»±ÈÆäËüÈκνű¾ÓïÑÔÑ·É«¡£

¡¡ÀýÈ磬ÀûÓÃPHPÔڽű¾Ö´ÐÐÆڼ䴦Àí¶ÁÈ¡Óû§ÌṩÊäÈ룬ʹÓãº

#!/usr/bin/php
function read_input()
{
$fp = fopen("/dev/stdin", "r");
$input = trim(fgets($fp, 255));
fclose($fp);
return $input;
}

printf("Please supply your name: ");
$name = read_input();
printf(" Hello, $name. ");
?>


ÉÏÃ涨ÒåµÄread_input()º¯Êý½«´ÓSTDINÌáÈ¡ÊäÈ룬´æ´¢ÓÚ$input ±äÁ¿ÖУ¬ÕûÀíËùÓÐÒýµ¼µÄºÍβËæµÄ¿Õ°×¿Õ¼ä£¬È»ºó·µ»Ø¡£

ͬÑùµÄÔ­Àí»¹¿ÉÒÔÓ¦ÓÃÓÚ¶ÁÈ¡ºÍ²Ù×÷±ê×¼Îļþ£»¼Çס£¬¶ÔÓÚLinux£¬STDINÖ»ÊÇÁíÒ»¸öÎļþ¶øÒÑ£¨ÏàÓ¦µØ£¬ÉÏÊöÀý×ÓÖеÄopening /dev/stdin Ò²ÊÇÕâÑù£©¡£

#!/usr/bin/php

if (file_exists($argv[1]))
{
$file = $argv[1];
} else {
printf("ERROR: File '$file' does not exist! ");
exit 1;
}
$data = file($file);
$c = 1;
foreach ($data as $line)
{
printf(sprintf("[%s]: %s", $c, $line));
$c++;
}

ÔÚÉÏÊöÀý×ÓÖУ¬PHP½Å±¾½«»á¶ÁÈ¡´«µÝµ½ÃüÁîÐÐÎļþÖеÄÿһÐУ¬²¢½ôË浱ǰÐÐÊý½«ÆäÊä³ö¡£Èç¹ûÎļþ²»´æÔÚ£¬½Å±¾»á´òÓ¡Ò»¸ö³ö´íÌáʾ£¬²¢½«·µ»Ø´úÂë1²¢Í˳ö£¨1±íʾ³ö´í£»ÔÚÕý³£ÔËÐÐÖУ¬½Å±¾»áÒÔ·µ»Ø´úÂë0Í˳ö£©¡£file() º¯ÊýÔÚÕâÀïʹÓã¬Ëü¶ÁÈ¡ÎļþµÄÿÐУ¬ÁгÉÊý×飨ÔÚÕâ¸öÀý×ÓÖУ¬Êý×éΪ$data£©£¬È»ºóÓÃÓÚforeach()Óï¾äÖУ¬ÔÚÊý×éÖÐÑ­»·£¬Ò»´ÎÊÇÎļþÖеÄÒ»ÐС£

PHPÒѾ­²»ÔÙÑϸñ¾ÐÄàÓÚ»ùÓÚWebµÄ±à³Ì¡£ÎÒÃÇ¿ÉÒÔ½«ËüÇáËÉÓ¦ÓÃÓÚ±àдÃüÁîÐнű¾£¬¼ÈÁé»îÓÖ¿ìËÙ¡£Í¬ÑùµØ£¬¼¸ºõËùÓÐÄãÄÜÓÃWeb½Å±¾½øÐвÙ×÷µÄ£¬ÀýÈ磬Êý¾Ý¿â²Ù×÷£¬¶¼¿ÉÒÔÇáËɵØÓ¦ÓÃPHPµÄÃüÁîÐнű¾Íê³É¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ