当前位置: 移动技术网 > IT编程>开发语言>PHP > php命令行写shell实例详解

php命令行写shell实例详解

2019年05月29日  | 移动技术网IT编程  | 我要评论

php 可以像java perl python 那样运行,今天发现如果我早早知道这个,或许我不会去学习java 和 python

当年学java不过为了一个程序放在服务器上,不停的跑啊跑,原来 php 也可以。

php -h
usage: php [options] [-f] <file> [--] [args...]
 php [options] -r <code> [--] [args...]
 php [options] [-b <begin_code>] -r <code> [-e <end_code>] [--] [args...]
 php [options] [-b <begin_code>] -f <file> [-e <end_code>] [--] [args...]
 php [options] -s <addr>:<port> [-t docroot] [router]
 php [options] -- [args...]
 php [options] -a
 -a    run as interactive shell
 -c <path>|<file> look for php.ini file in this directory
 -n    no configuration (ini) files will be used
 -d foo[=bar]  define ini entry foo with value 'bar'
 -e    generate extended information for debugger/profiler
 -f <file>  parse and execute <file>.
 -h    this help
 -i    php information
 -l    syntax check only (lint)
 -m    show compiled in modules
 -r <code>  run php <code> without using script tags <?..?>
 -b <begin_code> run php <begin_code> before processing input lines
 -r <code>  run php <code> for every input line
 -f <file>  parse and execute <file> for every input line
 -e <end_code> run php <end_code> after processing all input lines
 -h    hide any passed arguments from external tools.
 -s <addr>:<port> run with built-in web server.
 -t <docroot>  specify document root <docroot> for built-in web server.
 -s    output html syntax highlighted source.
 -v    version number
 -w    output source with stripped comments and whitespace.
 -z <file>  load zend extension <file>.
 args...   arguments passed to script. use -- args when first argument
     starts with - or script is read from stdin
 --ini   show configuration file names
 --rf <name>  show information about function <name>.
 --rc <name>  show information about class <name>.
 --re <name>  show information about extension <name>.
 --rz <name>  show information about zend extension <name>.
 --ri <name>  show configuration for extension <name>.

1.用php命令行的方式执行php脚本,例如/usr/bin/php test.php

缩写 php test.php

test.php

<?php
for($i=0;$i<10;$i++){
 echo $i;
 echo '\n';
}
?>

2.脚本开头第一行写上#!/usr/bin/php,然后可以把脚本设为可执行 chmod a+x test.php,之后就可以用命令行的方式直接执行脚本了,例如./test.php

#!/usr/bin/php
<?php
for($i=0;$i<10;$i++){
 echo $i;
 echo " java-er.com \n";
}
?>

执行一小时,看看php会不会挂,我希望一个命令行可以跑到天荒地老

#!/usr/bin/php
<?php
for($i=0;$i<360;$i++){
 echo $i;
 sleep(10);
 echo " java-er.com \n";
}
?>

 

3. 外部传入参数

#!/usr/bin/php
<?php
 var_dump($argc); //返回参数总个数
 var_dump($argv);
 
exit;
?>
./test.php

int(1)
array(1) {
 [0]=>
 string(10) "./test.php"
}
./test.php a java php

int(4)
array(4) {
 [0]=>
 string(10) "./test.php"
 [1]=>
 string(1) "a"
 [2]=>
 string(4) "java"
 [3]=>
 string(3) "php"
}

总结

以上所述是小编给大家介绍的php命令行写shell实例详解,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网