简体   繁体   中英

Running php script (php function) in linux bash

How we run php script using Linux bash?

php file test.php

test.php contains:

<?php echo "hello\n" ?>

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.

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

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

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:

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

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

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:

#!/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.

just run in linux terminal to get phpinfo .

   php -r 'phpinfo();'

and to run file like index.php

    php -f index.php

只需这样做:

php test.php
php -f test.php

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

php test.php

should do it, or

php -f test.php

to be explicit.

I was in need to decode URL in a Bash script. So I decide to use PHP in this way:

$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"

Sample output:

$ ./url-decode.sh 
url=https://1/ecp/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM