栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Unix/Linux中如何直接执行PHP脚本文件?

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unix/Linux中如何直接执行PHP脚本文件?

  使用Linux系统的服务器都有搭建完整的PHP环境,因此有些用户会用PHP去写一些执行自动化任务的脚本,可是发现每次执行PHP脚本都需要使用php myscript.php的方式,感觉有点麻烦。其实我们是可以直接执行PHP脚本文件的,但是具体该怎么操作呢?下面小编就给大家介绍下Unix/Linux中如何直接执行PHP脚本文件。

  编写你的脚本文件

  这里我们编写一个名字为 test_run.php 的文件,文件的内容如下:

  Here is some plain text.

  Here is the file name:

  《?php

  echo $argv[0], PHP_EOL;

  ?》

  脚本内容很简单,就是把当前脚本文件的名称打印出来。

  然后,我们使用 PHP 命令执行一下这个脚本:

  yuanyu@ymac:phpworkspace $ php test_run.php hello

  Here is some plain text.

  Here is the file name:

  test_run.php

  yuanyu@ymac:phpworkspace $

  给脚本文件增加头信息,并且设置权限

  然后,在这个文件的第一行写上 php 命令的全路径,前面是一个 #!:

  #!/usr/bin/php

  Here is some plain text.

  Here is the file name:

  《?php

  echo $argv[0], PHP_EOL;

  ?》

  然后给这个文件赋予可执行的权限:

  yuanyu@ymac:phpworkspace $ chmod u+x 。/test_run.php

  接下来就可以直接执行这个脚本了:

  yuanyu@ymac:phpworkspace $ 。/test_run.php

  Here is some plain text.

  Here is the file name:

  。/test_run.php

  yuanyu@ymac:phpworkspace $

  这种方式在 PHP 官方文档中也是有说的,请参考:

  http://php.net/manual/en/features.commandline.usage.php

  文档中的

  “Example #2 script intended to be run from command line (script.php)”

  以上就是Unix/Linux中直接执行PHP脚本文件的操作方法,不熟悉的用户可以参照上面介绍的具体步骤来操作。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/587921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号