Since our servers are all Linux systems and have a complete PHP environment, I sometimes use PHP to write scripts to automate tasks, but every time I execute this php script I need to use PHP myscript.php, a little bit? Long F 涫 bear the strong shoot of benzene and onion PHP script files.
Write your script file
Here we write a file with a name of test_run.php, and the contents of the document are as follows:
This is some plain text.
This is the file name:
<?php
echo $argv [0], Php_eol;
?>
The script content is simple, which is to print out the name of the current script file.
We then use the PHP command to execute this script:
Yuanyu@ymac:phpworkspace $ php test_run.php Hello
This is some plain text.
This is the file name:
test_run.php
Yuanyu@ymac:phpworkspace $
Add header information to the script file and set permissions
Then, on the first line of the file, write the full path to the PHP command, preceded by a #!:
#!/usr/bin/php
This is some plain text.
This is the file name:
<?php
echo $argv [0], Php_eol;
?>
The file is then given the executable permission:
Yuanyu@ymac:phpworkspace $ chmod u+x./test_run.php
You can then execute the script directly:
Yuanyu@ymac:phpworkspace $./test_run.php
This is some plain text.
This is the file name:
./test_run.php
Yuanyu@ymac:phpworkspace $
This approach is also stated in the PHP official documentation, please refer to:
http://php.net/manual/en/features.commandline.usage.php
In the document
"Example #2 Script intended to is run from command line (script.php)"