在linux bash中运行php脚本(php函数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5549562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:37:24  来源:igfitidea点击:

running php script (php function) in linux bash

phplinuxbashshell

提问by chrithccmc

How we run php script using linux bash ?

我们如何使用 linux bash 运行 php 脚本?

php file test.php

php 文件 test.php

test.php contain.

test.php 包含。

<?php echo "hello\n" ?>

采纳答案by Dunhamzzz

From the command line, enter this:

从命令行输入:

php -f filename.php

Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.

确保 filename.php 包含并执行要测试的函数。您回显的任何内容都会出现在控制台中,包括错误。

Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).

请注意,Apache PHP 的 php.ini 通常与 CLI PHP(命令行界面)不同。

Reference: https://secure.php.net/manual/en/features.commandline.usage.php

参考:https: //secure.php.net/manual/en/features.commandline.usage.php

回答by deceze

Simply this should do:

简单地这应该做:

php test.php

回答by Mark Baker

php -f test.php

See the manualfor full details of running PHP from the command line

有关从命令行运行 PHP 的完整详细信息,请参阅手册

回答by nicja

php test.php

should do it, or

应该这样做,或者

php -f test.php

to be explicit.

要明确。

回答by Ali

First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:

首先检查您的 PHP 安装是否支持 CLI。类型:php -v。您可以通过两种方式从命令行执行 PHP:

  1. php yourfile.php
  2. php -r 'print("Hello world");'
  1. php yourfile.php
  2. php -r 'print("Hello world");'

回答by Darhuuk

There are two ways you can do this. One is the one already mentioned, i.e.:

有两种方法可以做到这一点。一种是已经提到的,即:

php -f filename.php

The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:

第二个选项是使脚本可执行 ( chmod +x filename.php) 并将以下行添加到 .php 文件的顶部:

#!/path/to/php

I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.phpthan having to type php -f /path/to/phpfile.phpevery time.

我不确定网络服务器是否喜欢这个,所以如果你还想在网站中使用 .php 文件,那可能不是最好的主意。尽管如此,如果您只是在编写某种脚本,那么输入./path/to/phpfile.phpphp -f /path/to/phpfile.php每次都输入要容易。

回答by Waqas Rana

just run in linux terminal to get phpinfo .

只需在 linux 终端中运行即可获取 phpinfo 。

   php -r 'phpinfo();'

and to run file like index.php

并运行像 index.php 这样的文件

    php -f index.php