Call php function from bash - with arguments

余生颓废 提交于 2021-02-19 05:20:25

问题


I have a simple func.php file with concat function:

<?php
function concat($arg1, $arg2)
    {
        return $arg1.$arg2;
    }
?>

I would like to call this function from linux bash shell with two arguments :

1st: "Hello, "
2nd: "World!"

and print the output ("Hello, World!") to linux bash shell.

Please tell me, how to do it?


回答1:


What you want is $argv

So for example, your script would be called like this:

php /path/to/script.php 'Hello, ' 'World!'

Then in your PHP file:

<?php
$arg1 = $argv[1];
$arg2 = $argv[2];

echo concat($arg1, $arg2);

function concat($arg1, $arg2) {
    return $arg1 . $arg2;
}


来源:https://stackoverflow.com/questions/27090760/call-php-function-from-bash-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!