Home  >  Article  >  Backend Development  >  php command line interface

php command line interface

WBOY
WBOYOriginal
2016-08-08 09:20:48983browse

常用选项
php -v
php -i PHP安装的有关信息
php -h 访问帮助文件
php -m 列出编译到当前PHP安装的所有模块

执行一段代码
php -r 'echo "hello, world!";'
php -r 'echo "Hello, World!\n";'
php -r '$ts = filemtime("/data/www/index.html");
> echo date("F j Y H:clear:s", $ts);'

php -r 'echo "Hello, world! \n\n";'

交互
php -a
echo 'hello world';
$arr = ['who', 'what', 'when'];
echo $arr[1];

创建命令行脚本
vim dummy.php

```
#!/usr/bin/php
<?php

// do whatever.
?>
This text is also displayed.
<?php

?>
```

php dummy.php


vim number.php
```
#!/usr/bin/php
<?php
$file = 'readme.txt';
echo "\nNumbering the file named '$file' ----------------\n\n";

$data = file($file);

$n = 1;

foreach($data as $line) {
	echo "$n $line";
	$n++;
}

echo "\n $file 文件结束\n";
```
运行的方式
php number.php
php -f number.php
chmod +x number.php
./number.php

检查语法, 但是不能检查严重错误
php -l number.php


使用命令行参数
vim number2.php
```
#!/usr/bin/php
<?php
if($_SERVER['argc'] == 2) {
	$file = $_SERVER['argv'][1];
	if(file_exists($file) && is_file($file)) {
		echo "\nNumbering the file named '$file' ----------------\n\n";
		if( $data = file($file) ){
		$n = 1;

		foreach($data as $line) {
			echo "$n $line";
			$n++;
		}

		echo "\n $file 文件结束\n";
			exit(0);
		} else {
			echo "文件不能读取\n";
		}
	} else {
		echo "文件不存在.\n";
		exit(1);
	}
} else {
	echo "\nUsage: number2.php <filename>\n\n";
	exit(1);
}
```

php number2.php filename

接受输入
vim init.php
```
#!/usr/bin/php
<?php

echo "\n请输入安装的一个模式?\n\n[0]开发模式\n[1]线上模式\n你选择[0-1, 或者\"q\"退出]";

if(fscanf(STDIN, '%d', $mode)==1) { //如果返回的结果不是1, 也就是没有读取一个数字, 脚本会执行else
	if($mode==0) {
		echo "\n你选择了开发模式, 进行开发模式配置\n";
	} else {
		echo "\n你选择了开发模式, 进行开发模式配置\n";
	}
} else {
    echo "\n请输入一个数字进行选择\n";
}
```


内置服务器

php -h  如果有大S和-t选项表示支持内置服务器  -t 指定网站根目录
php -S localhost:8080
php -S localhost:8080 -t /data/www

然后可以在浏览器中访问, 或者通过curl访问

The above has introduced the PHP command line interface, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn