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

时间:2022-05-25 00:08:25

How we run php script using linux bash ?

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

php file test.php

php文件test.php

test.php contain.

测试。php包含。

<?php echo "hello\n" ?>

7 个解决方案

#1


34  

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.

确保文件名。php包括并执行您想要测试的函数。您所回显的任何内容都将出现在控制台中,包括错误。

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

注意php。Apache 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

#2


22  

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.php than having to type php -f /path/to/phpfile.php every time.

我不确定网络服务器是否喜欢这个,所以如果你也想在网站中使用。php文件,这可能不是最好的方法。不过,如果您只是编写某种脚本,那么输入./path/to/phpfile会更容易。php而不是必须输入php -f /path/to/phpfile。每次php。

#3


21  

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 yourfile.php
  3. php -r 'print("Hello world");'
  4. php - r的打印(“Hello world”);

#4


4  

Simply this should do:

只是这应该做的是:

php test.php

#5


2  

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

#6


1  

php -f test.php

See the manual for full details of running PHP from the command line

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

#7


1  

php test.php

should do it, or

应该做的,还是

php -f test.php

to be explicit.

是明确的。

#1


34  

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.

确保文件名。php包括并执行您想要测试的函数。您所回显的任何内容都将出现在控制台中,包括错误。

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

注意php。Apache 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

#2


22  

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.php than having to type php -f /path/to/phpfile.php every time.

我不确定网络服务器是否喜欢这个,所以如果你也想在网站中使用。php文件,这可能不是最好的方法。不过,如果您只是编写某种脚本,那么输入./path/to/phpfile会更容易。php而不是必须输入php -f /path/to/phpfile。每次php。

#3


21  

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 yourfile.php
  3. php -r 'print("Hello world");'
  4. php - r的打印(“Hello world”);

#4


4  

Simply this should do:

只是这应该做的是:

php test.php

#5


2  

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

#6


1  

php -f test.php

See the manual for full details of running PHP from the command line

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

#7


1  

php test.php

should do it, or

应该做的,还是

php -f test.php

to be explicit.

是明确的。