How to directly execute PHP script files in Unix/Linux?

  
                

Servers using Linux have built-in PHP environments, so some users will use PHP to write scripts that perform automated tasks, but find that every time you execute a PHP script, you need to use php myscript.php. trouble. In fact, we can directly execute the PHP script file, but how to do it? The following small series will introduce you how to directly execute PHP script files in Unix/Linux.

write your script file

we write a name for test_run.php file here, the contents of the file are as follows:.

Here is some plain text < Br>

Here is the file name:

"? Php

echo $argv[0], PHP_EOL;

? 》

The content of the script is very simple, that is, print the name of the current script file.

Then, we execute this script using PHP commands:

yuanyu@ymac:phpworkspace $ php test_run.php hello

Here is some plain text.

Here is the file name:

test_run.php

yuanyu@ymac:phpworkspace $

Add header information to the script file and set permissions

Then, write the full path of the php command on the first line of this file, in front of a #! :

#! /usr/bin/php

Here is some plain text.

Here is the file name:

"? Php

echo $argv[0], PHP_EOL;

?

Then give this file permission to execute:

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

The script can be executed directly:

yuanyu@ymac:phpworkspace $ . /test_run.php

Here is some plain text.

Here is the file name:

. /test_run.php

yuanyu@ymac:phpworkspace $

This method is also mentioned in the official PHP documentation, please refer to:

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


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

The above is the operation method of directly executing PHP script files in Unix/Linux. Unfamiliar users can refer to the specific steps described above.

Copyright © Windows knowledge All Rights Reserved