note 125955 added to features.commandline
| From: | Anonymous@php.net | Date: | Wed, 24 Mar 2021 12:49:57 +0000 |
| Subject: | note 125955 added to features.commandline | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219200@lists.php.net to get a copy of this message | ||
We can pass many arguments directly into the hashbang line.
As example many ini setting via the -d parameter of php.
---
#!/usr/bin/php -d memory_limit=2048M -d post_max_size=0
phpinfo();
exit;
---
./script | grep memory
memory_limit => 2048M => 2048M
---
But we can also use this behaviour into a second script, so it call the first as an interpreter, via
the hashbang:
---
#!./script arg1 arg2 arg3
---
However the parameters are dispatched in a different way into $argv
All the parameters are in $argv[1], $argv[0] is the interpreter script name, and $argv[1] is the
caller script name.
To get back the parameters into $argv, we can simply test if $argv[1] contains spaces, and then
dispatch again as normal:
#!/usr/bin/php -d memory_limit=2048M -d post_max_size=0
<?php
var_dump($argv);
if (strpos($argv[1], ' ') !== false){
$argw = explode(" ", $argv[1]);
array_unshift($argw, $argv[2]);
$argv = $argw;
}
var_dump($argv); ?>
---
array(3) {
[0]=>
string(8) "./script"
[1]=>
string(15) "arg1 arg2 arg3 "
[2]=>
string(14) "./other_script"
}
array(4) {
[0]=>
string(8) "./other_script"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
---
This will maintain the same behaviour in all cases and allow to even double click a script to call
both parameters of another script, and even make a full interpreter language layer. The other
script doesn't has to be php. Take care of paths.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.245 (proxied: 37.167.127.95)
----
Manual Page -- http://php.net/manual/en/features.commandline.php
Edit -- https://master.php.net/note/edit/125955
Del: integrated -- https://master.php.net/note/delete/125955/integrated
Del: useless -- https://master.php.net/note/delete/125955/useless
Del: bad code -- https://master.php.net/note/delete/125955/bad+code
Del: spam -- https://master.php.net/note/delete/125955/spam
Del: non-english -- https://master.php.net/note/delete/125955/non-english
Del: in docs -- https://master.php.net/note/delete/125955/in+docs
Del: other reasons-- https://master.php.net/note/delete/125955
Reject -- https://master.php.net/note/reject/125955
Search -- https://master.php.net/manage/user-notes.php